MVC Paging and Sorting Patterns: How to Page or Sort Re-Using Form Criteria

Posted by CRice on Stack Overflow See other posts from Stack Overflow or by CRice
Published on 2010-04-26T06:12:02Z Indexed on 2010/04/26 6:13 UTC
Read the original article Hit count: 292

What is the best ASP.NET MVC pattern for paging data when the data is filtered by form criteria?

This question is similar to: http://stackoverflow.com/questions/1425000/preserve-data-in-net-mvc but surely there is a better answer?

Currently, when I click the search button this action is called:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Search(MemberSearchForm formSp, int? pageIndex, string sortExpression)
    {}

That is perfect for the initial display of the results in the table.

But I want to have page number links or sort expression links re-post the current form data (the user entered it the first time - persisted because it is returned as viewdata), along with extra route params 'pageIndex' or 'sortExpression',

Can an ActionLink or RouteLink (which I would use for page numbers) post the form to the url they specify?

<%= Html.RouteLink("page 2", "MemberSearch", new { pageIndex = 1 })%>

At the moment they just do a basic redirect and do not post the form values so the search page loads fresh.

In regular old web forms I used to persist the search params (MemberSearchForm) in the ViewState and have a GridView paging or sorting event reuse it.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about paging