I would like to set a default sort in my grid read action on the controller and have it show the arrow in the grid UI. The sort appears to be applied correctly, but it doesn't show the column as sorted in the grid UI after the read.
Right now I not doing anything special with the grid Razor and if possible i would rather not attach any grid events, but I will do it if there is no other way.
Controller:
public async Task<
ActionResult
> Grid_Read([DataSourceRequest] DataSourceRequest request)
{
IQueryable<
GridItem
> list = null;
try
{
if (GridItemHelper.CanView(this) == false)
{
ModelState.AddModelError("read", "You cannot list GridItem");
}
else
{
list = _Db.GridItem.Select(s=>s); //this gets me the iqueryable
}
}
catch (Exception exc)
{
ModelState.AddModelError("read", "There was an error populating the GridItem Grid. MSG:" + exc.ToLogString(this));
}
//set the default filters
if(request.Filters == null || request.Filters.Count == 0) //it is best practice to use the kendo filters so it shows up correctly in the UI
{
}
//set the default sort
if(request.Sorts == null || request.Sorts.Count == 0)
{
request.Sorts = new List<
SortDescriptor
>() { new SortDescriptor("Id", System.ComponentModel.ListSortDirection.Descending) }; //sort by Id if there is no sort specified
}
var rList = list == null ? new List<
GridItem
>().ToDataSourceResult(request, ModelState) : list.ToDataSourceResult(request, ModelState); //by calling ToDataSourceResult from the IQueryable it will not pull everything down before filtering
var jsonResult = Json(rList);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}