views:

1132

answers:

3

I have a situation that I couldn't find a solution for through my searches on here. Here is the scenario:

I have a search form with 2 required fields and multiple optional ones. The form posts to an action method that determines which fields are selected and builds a List<> of objects that match the search criteria. I then pass that List<> to the view for display.

This issue I am running into involves how paging is typically done with asp.net mvc. For past projects I have used a custom Html helper that creates links which include the query parameters as well as a "page" parameter. It then uses a GET request and the .Take().Skip() format.

I've hit a wall on this project as I can't use a GET request for the search criteria and I can't figure out a way to keep the List<> in memory to do the usual "page" parameter trick.

I thought about storing the List<> in the session but the objects and the list could be very large.

I would think this is a popular issue with advanced search forms but I can't seem to find a good solution. Any help would be appreciated. Thanks!

A: 

Put everything in the same form: the required fields, the optional fields, the page links.

Two possibilities:

  1. Use submit buttons or images instead of anchor tags for the page links each having a different name (e.g. page1, page2, ...): this will allow you to get the desired page when the form is submitted.
  2. Put a hidden field inside your form. Then add a javascript click handler to any of the page anchors. This handler will update the value of the hidden field with the page, submit the form and cancel the event.

So clicking on any of the pager links will submit the form with all the data you need to build the list and pager links.

Darin Dimitrov
I have everything in the same form. In my action method I make all the optional parameters nullable and do some simple validation for the required fields. I'm also trying to avoid to the whole re-submit for every page as the logic I'm using to filter my objects by the search criteria is pretty hefty. Thanks for you response!
DM
So what's the problem?
Darin Dimitrov
The problem is that I need to be able to page trough a list of complex objects without using a query string and re-building the list every time a new page is requested. Not sure if it's possible.
DM
+1  A: 

If I understand properly, you only want to load the search results one time, and then page through them.

Have you looked into any jQuery paging functionality? You can just dump the entire list to the page and use JavaScript to handle the paging (and sorting if you like).

An example can be found at http://beckelman.net/demos/jqueryTableSorterConPaging/default.aspx

Jon Freeland
Thanks, this will probably be the direction I'll go. Just curious though if it's possible without JavaScript?
DM
If you don't want to use JavaScript, you will need to use some sort of caching (as explained in another answer), as well as communicate page changes to the server. Since you want to avoid GET, you can just wrap the paging links in a form and POST, which will run .Take().Skip() on the cache from the original search.
Jon Freeland
+2  A: 

How about cacheing the search result object and giving it a unique key. You would then have your paging links reference that unique (SearchID) and have your action look for that object, pull it from cache and Skip/Take from there.

This will not rebuild the object for every request, making page loading much faster and reducing strain on your database/application.

Here is a article about cacheing:

http://aspnet.4guysfromrolla.com/articles/100902-1.aspx

Here is a video about cacheing:

http://www.asp.net/learn/Videos/video-6206.aspx

Note: Be sure you specify expiration date on the cached object.

Baddie
Very helpful links, thanks!
DM