This is a migrated thread and some comments may be shown as answers.
how to retrieve sort parameters
4 Answers 190 Views
This is a migrated thread and some comments may be shown as answers.
jc mag
Top achievements
Rank 1
jc mag asked on 16 Feb 2012, 06:36 PM
In an ASP.NET MVC application, how could I retrieve the sort field and direction?
I see that the grid sends these parameters: sort[0][dir] and sort[0][field] but how could I retrieve them in c#?

public JsonResult GetLogs(int page, int pageSize, int skip, int take, ???)

or should I use the parameterMap setting?
parameterMap: function(options) { 
          return
             pageIndex: options.page, 
             size: options.pageSize, 
             orderBy: convertSort(options.sort) 
          
       

but I don't find the implementation of convertSort, do you have an example?

4 Answers, 1 is accepted

Sort by
0
Boone
Top achievements
Rank 2
answered on 16 Feb 2012, 08:21 PM
I just had this problem, here is what I did:

JS:
parameterMap: function (options) {
   var object = {};
   object.pageIndex = options.page;
   object.size = options.pageSize;
   object.orderBy = options.sort;
   return JSON.stringify({ options: object, criteria: searchCriteria });
}

C#
[OperationContract]
public ReturnObject GetResults(GridOptions options)
{
  //Do something
}
 
public class GridOptions
{
   public int pageIndex { get; set; }
   public int size { get; set; }
   public IEnumerable<GridSort> orderBy { get; set; }
}
 
public class GridSort
{
   public string field { get; set; }
   public string dir { get; set; }
}


0
Roel
Top achievements
Rank 1
answered on 18 Feb 2012, 08:18 AM
Boone,
Can you give code sample what you did in the //Do something area. I want to see how you do it, if you dont mind.
Thanks
Roel
0
Boone
Top achievements
Rank 2
answered on 18 Feb 2012, 08:34 PM
I pass it all the way down to my data layer and call a stored procedure that only returns that specified range of data.

cmd.Parameters.AddWithValue("@PageIndex", options.pageIndex);
cmd.Parameters.AddWithValue("@PageSize", options.size);
  
var sortOrders = options.orderBy.ToList();
for (var i = 1; i >= sortOrders.Count; i++)
{
    cmd.Parameters.AddWithValue("@SortDirection"+i, sort.dir);
    cmd.Parameters.AddWithValue("@SortField"+i, sort.field);
    if(i == 2)//we only allow up to two sorts
    {
       break;
    }
}

The stored proc then does its sorting. Starts at (PageIndex + PageSize) and selects top(PageSize). Then returns the next set of data to my grid to display. My stored proc also has an 'out' parameter that does a count of the total results before chopping the data so that my grid knows how many pages to display.

Hope this helps.
0
Vin
Top achievements
Rank 1
answered on 18 Feb 2012, 11:36 PM
Boone / jc mag / Roel

Were you able to display the sort direction icon on the grid on the initial load. The sort direction icon is getting set when we click on a column. How do we specify the default column to display the sort direction icon? If you guys got the initial sort icon working , please let me know.

Boone's code for getting the sort dir and column worked for me. Thanks you.

parameterMap:

 

function (options) {
    var object = {};
    object.pageSize = options.pageSize;
    object.page = options.page;
    object.skip = options.skip;
    object.take = options.take;
    object.orderBy = options.sort;
    return JSON.stringify({ options: object });
}
And in the controller

 

public

 

 

JsonResult IndexJson(GridOptions options){

 

    IEnumerable

 

 

<GridSort> sds = options.orderBy;
    GridSort sd = null
    if (sds != null)
    {
        sd = options.orderBy.FirstOrDefault<
GridSort>(); 
    }
}


 

 

Tags
Grid
Asked by
jc mag
Top achievements
Rank 1
Answers by
Boone
Top achievements
Rank 2
Roel
Top achievements
Rank 1
Vin
Top achievements
Rank 1
Share this question
or