This is a migrated thread and some comments may be shown as answers.

set sort in ASP.Net Controller so it shows in Grid UI

3 Answers 33 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Logan
Top achievements
Rank 1
Veteran
Logan asked on 30 Jun 2015, 01:14 PM

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;
        }

3 Answers, 1 is accepted

Sort by
0
Boyan Dimitrov
Telerik team
answered on 02 Jul 2015, 12:30 PM

Hello Logan,

Please refer to this forum discussion that shows how to set default sort for the Kendo Grid on initial load. 

Regards,
Boyan Dimitrov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Logan
Top achievements
Rank 1
Veteran
answered on 02 Jul 2015, 01:35 PM

Thanks for getting back to me.

There are times when the default sort is calculated, so it isn't as simple as setting a starting sort.  So does that mean that there is no way to set it in the controller and have it show up in the grid UI?

 

0
Boyan Dimitrov
Telerik team
answered on 06 Jul 2015, 10:26 AM

Hello Logan,

 

In case when server operations are enabled setting a sorting expression to the data source will make a request to the server (read method in the controller). 

 

Given this if the sort expression comes with the response and then sort the data source it will initiate another request to the server and so on. 

 

A possible solution might be setting the sort expression for the data source on the client-side only once. The sort expression could be retrieved by a different jQuery ajax request to the server. 

 

Regards,
Boyan Dimitrov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Logan
Top achievements
Rank 1
Veteran
Answers by
Boyan Dimitrov
Telerik team
Logan
Top achievements
Rank 1
Veteran
Share this question
or