Search code examples
c#asp.net-mvc-4paginationwebgrid

Keep current page when sorting in @grid.GetHtml


Is there any way to keep current page when sorting @grid.GetHtml columns?

For now I have this

public virtual ActionResult Users(int? page)
{
  var model = _context.Users();
  return View(model);
}

and HTML

@{
 var grid = new WebGrid(source: Model,defaultSort: "LastActivity",rowsPerPage: 20);

 // Force a descending sort only when no user specified sort is present
 if (Request.QueryString[grid.SortDirectionFieldName].IsEmpty())
 {
    grid.SortDirection = SortDirection.Descending;
 } 
}
@if (Model != null)
{
  @grid.GetHtml(tableStyle: 
....

Thank you!


Solution

  • WebGrid does not have a built-in solution for this. You'll have to create your own way to load the current page during a sort action.

    Here is one possible way: http://forums.asp.net/post/4220540.aspx