asp.net-mvcpagination

ASP.NET MVC paging, when you have multiple criteria


What would be the best-practice implementation when visualizing a list, driven by multiple criteria, in ASP.NET MVC?

In other words - I am visualizing a very large list of items. Hence - I have already implemented (a generic) paging, which works great. However, I also have an optional search form, which filters the items based on multiple criteria. How would I go about implementing paging, which preserves the search criteria? The obvious answer is with Routing, however, I don't want to pass 10-20 different parameters in the URL. What's my alternative? Form posting?

TL;DR; - Paging with multiple filter criteria, without necessarily creating a Routing for each filter.


Solution

  • I would advise you to stay away from routing as that can quickly evolve into an untestable headache. Using POST may not be wise either since it is not very user friendly as they cannot bookmark URLs or use their back button (without reposting) as expected. The best way is to use GET with querystring as you mentioned.

    To avoid huge URLs (which shouldn't usually be an issue since URLs generally can go up to 2,000 characters), you can just use default and empty filter values to your advantage. When creating and passing the filter values, just omit the ones that contain default or empty values and implicitly set them in your code. This will rid your URLs of many empty params such as example.com/?query=somestring&p1=&p2=&p3=&p4= and reduce it to example.com/?query=somestring.

    Another random piece of advice, make sure you create a proper ViewModel to contain all the logic pertaining to filter and paging params and their defaults and pass it in as a param in your HttpGet action method.