jQuery datatable in MVC (server-side)

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.

Answers


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();
    //...
}

Need Your Help

addClass if ul exists (jQuery)

javascript jquery menu drop-down-menu

I'm building a simple dropdown where I'd like to add a class to parent if UL exists:

unable to compare data from database to array of strings

javascript php html mysql arrays

i have a program where i need to insert words as much as i wish and then those words will be checked through database, if it is present in database , it should return how many words where present in

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.