I’ve read several different posts on paging w/ in MVC but none describe a scenario where I have something like a search form and then want to display the results of the search criteria (with paging) beneath the form once the user clicks submit.
My problem is that, the paging solution I’m using will create <a href=”…”> links that will pass the desired page like so: http://mysite.com/search/2/ and while that’s all fine and dandy, I don’t have the results of the query being sent to the db in memory or anything so I need to query the DB again.
If the results are handled by the POST controller action for /Search and the first page of the data is rendered as such, how do I get the same results (based on the form criteria specified by the user) when the user clicks to move to page 2?
Some javascript voodoo? Leverage Session State? Make my GET controller action have the same variables expected by the search criteria (but optional), when the GET action is called, instantiate a FormCollection instance, populate it and pass it to the POST action method (there-by satisfying DRY)?
Can someone point me in the right direction for this scenario or provide examples that have been implemented in the past? Thanks!
My method is to have an Action that handles both the post and the get scenarios.
This is my which can be handled by both GET and POST methods:
The
eventFilterFormis a presentation model that contains someIEnumerable<SelectListItem>properties for my search form.The
eventIndexModelis a presentation model that combines theeventFilterFormand the results of the search –eventsThe
eventsis a special type ofIPagedList. You can get more information and code for that here and here. The first link talks about IPagedList where as the second link has an Advanced Paging scenario which you should need.The advanced paging has the following method that I use:
And I use it like so:
This creates links that look like:
HTHs,
Charles
Ps.
EvalWithModelStateis below:PPs. If you are going to put dates into get variables – I would recommend reading my blog post on it… 🙂