سؤال

https://datatables.net/usage/server-side

On the page above, there are parameters that you need to receive to make server-side datatable work. I have a helper class

public class TableParameter
{
    public string sEcho { get; set; }
    public int iDisplayStart { get; set; }
    public int iDisplayLength { get; set; }
    public int iSortingCols { get; set; } 
}

But in order to sort columns I need to receive

 string sSortDir_(int)

How do I do that? I know (int) represents column ID that needs to be sorted, but I just can't catch it in my controller.

هل كانت مفيدة؟

المحلول

The datatable will post one or more sSortDir_x parameters to your controller, depending on how many columns are sorted on simultaneously in the table.

The specific columns that the table is sorted by are sent in the iSortCol_ parameters (again, one or more).

public class TableParameter
{
    public string sEcho { get; set; }
    public int iDisplayStart { get; set; }
    public int iDisplayLength { get; set; }
    public int iSortingCols { get; set; } 
    public int iSortCol_0 { get; set; } // the first (and usually only) column to be sorted by
    public string sSortDir_0 { get; set; } // the direction of the first column sort (asc/desc)
    public int iSortCol_1 { get; set; } // the second column to be sorted by
    public string sSortDir_1 { get; set; } // the direction of the second column sort
    // etc
}

نصائح أخرى

For receiveing a column name in action, that is used for one-column sorting:

public ActionResult SomeMethod(FormCollection coll)
{
    var sortingColumnNumber = Convert.ToInt32(coll["iSortCol_0"]);
    var sortingColumnName = coll[string.Format("mDataProp_{0}", sortingColumnNumber)]; 
    var propertyInfo = typeof(SomeObject).GetProperty(sortingColumnName);

    //..get List<SomeObject> sortedObjects
    sortedObjects = sortedObjects.OrderBy(x => propertyInfo.GetValue(x, null)).ToList();
    //...
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top