I can't perform a aggregation on a grid using local binding

1 Answer 63 Views
Grid
Pablo
Top achievements
Rank 2
Pablo asked on 05 Aug 2022, 08:35 PM | edited on 05 Aug 2022, 08:43 PM

This is my code:

Index.cshtml

@model IEnumerable<ManagePartsViewModel> @(Html.Kendo().Grid(Model) .Name("grid-parts") .Columns(columns => { columns.Bound(f => f.Identifier).Hidden(); columns.Bound(f => f.PartType); columns.Bound(f => f.PartNumber); columns.Bound(f => f.PartName); columns.Bound(f => f.Barcode); columns.Bound(f => f.Warehouse); columns.Bound(f => f.Quantity); }) .DataSource(dataSource => dataSource .Ajax() .Model(model => { model.Id(p => p.Identifier); model.Field(p => p.PartType); model.Field(p => p.PartNumber); model.Field(p => p.PartName); model.Field(p => p.Barcode); model.Field(p => p.Warehouse); model.Field(p => p.Quantity); }) .Aggregates(aggregates => { aggregates.Add(p => p.PartType).Count(); }) .Group(groups => { groups.Add(p => p.PartType); }) .PageSize(8) .ServerOperation(false) ) .Groupable() .Pageable() .NoRecords() .Deferred() )

@section scripts
{
@Html.Kendo().DeferredScripts()
}

ManagePartsViewModel.cs

public class ManagePartsViewModel
{
    public int Identifier { get; set; }
    public string PartType { get; set; }
    public string PartNumber { get; set; }
    public string PartName { get; set; }
    public string Barcode { get; set; }
    public string Warehouse { get; set; }
    public int Quantity { get; set; }
}

HomeController.cs

public class HomeController : Controller
{
    private readonly IPartsService _partsService;
    private readonly ILogger<HomeController> _logger;

    public HomeController(IPartsService partsService, ILogger<HomeController> logger)
    {
        _partsService = partsService;
        _logger = logger;
    }

    public async Task<ActionResult<IEnumerable<ManagePartsViewModel>>> Index(IEnumerable<ManagePartsViewModel> viewModel)
    {
        try
        {
            viewModel = await _partsService.GetParts();
            return View(viewModel);
        }
        catch (Exception ex)
        {
            _logger.LogCritical(ex, ex.Message);
            return BadRequest();
        }
    }
}

Throws me this exception:

An unhandled exception occurred while processing the request.

ArgumentException: Invalid property or field - 'PartType' for type: AggregateFunctionsGroup

1 Answer, 1 is accepted

Sort by
0
Yanislav
Telerik team
answered on 10 Aug 2022, 10:37 AM

Hello Pablo,

Thank you for the information and details provided. 

I've reviewed the code snippets you shared, but I didn't notice anything wrong. Based on the shared information, I could not find anything that might be a possible reason for throwing the error.

However, since the provided details are not enough for me to be sure what is causing the observed behavior, I went ahead and tested the behavior. I've used a similar configuration to the one you've shared and on my side, everything seems to be working correctly.
https://www.screencast.com/t/9Gf1Wjsu

What I can recommend to you at this point is to check the used service (IPartsService _partsService) that returns the data as the problem may be coming from there. Other than that, everything with the configuration seems fine so, please make the needed checks at your end and let me know the results. In case you are still unable to find the reason for the problem, may I ask you to share more information about how to reproduce the problem? Even better would be if you can reproduce the issue in a sample project and send it to me. Thus, I will be able to investigate the problem and try to find a possible solution.

Regards,
Yanislav
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Pablo
Top achievements
Rank 2
Answers by
Yanislav
Telerik team
Share this question
or