Search code examples
asp.net-mvcsortingpaginationjquery-ui-sortable

ASP.NET MVC. Clean way to implement persistant sorting?


I still haven`t found any example of sorting implementation through paging in ASP.NET MVC
(sort by name->press page 2->page 2 is still sorted by name).

I could "hack" it, make it dirty, but i`m sure there have to be good "how-to" guides for this.

What about sorting by two columns?


Solution

  • Just keep the sort expression in your model view and write it to the pagination route links.
    For example like:

    /MyEntity/Page/2?sort=Name
    

    Or with custom routing like:

    /MyEntity/Page/2/Name
    

    For the latter the route mapping would look like:

    {controller}/Page/{pageIndex}/{sortExpression}