Perry
Perry

Reputation: 2282

Kendo Grid DataSourceRequest ArgumentException trying to sort

I am using this code to get my data and push it to the Kendo Grid

public ActionResult Read([DataSourceRequest] DataSourceRequest request)
{
    return Json(GetData(request), JsonRequestBehavior.AllowGet);
}

private DataSourceResult GetData(DataSourceRequest request)
{
    var Items = _db.Item.Local.ToDataSourceResult(request, x => new
    {
        ID = x.ID,
        Title = x.Title,
        LastEdited = x.User.LoginName,
        Category = x.CategoryItem.Title,
        DateEdited = x.DateEdited
    });
    return Items;
}

Using no model in the view for the Grid, letting the Grid figure everything out. This all works and I can browse through pages too. However the moment I add in a sorting method it throws me this exception for example:

Invalid property or field - 'Category' for type: Item

And each subsequent request fails because it has the sort in the DataSourceRequest until I reload the page to clear the request. Am I missing something here in terms of configuration that I need to add to this code?

Upvotes: 2

Views: 5080

Answers (1)

Hung Doan
Hung Doan

Reputation: 1167

yeah, because your Grid tell DataSourceResult that "I'll sort by Category ".

But in your DataSource (_db.Item.Local) ,It doesn't have any field named 'Category' .

(your x => new{Category = x.CategoryItem.Title,} just select Category after the 'Sorting' had been completed).

So, you can try something like :

_db.Item.Local.Select(x=> new {
    Category = x.CategoryItem.Title
    }).ToDataSourceResult(request)

Upvotes: 8

Related Questions