Paging search results with asp.net MVC
Asked Answered
D

3

5

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!

Diego answered 2/11, 2009 at 20:50 Comment(1)
Why don't you write a helper or extension to do the paging and use it in your view itself ?, I am just talking from the top of my head .. thanksSuggs
G
3

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:

https://web.archive.org/web/20211020111559/https://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.

Goeselt answered 2/11, 2009 at 22:57 Comment(2)
@AndreiMikhalevich Is there a better approach when you don't want to re-query your data source?Sylvie
@Alexandra, if you don't want to re-query your data source, obviously you should save your search results somewhere. It's all about caching. You can save it inside session, file, application storage. It's up to you where to store it. But you should store it anyway.Hakeem
P
2

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

Photoactive answered 2/11, 2009 at 21:11 Comment(2)
Thanks, this will probably be the direction I'll go. Just curious though if it's possible without JavaScript?Diego
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.Photoactive
T
1

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.

Testes answered 2/11, 2009 at 20:58 Comment(2)
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!Diego
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.Diego

© 2022 - 2024 — McMap. All rights reserved.