How do you implement paging in ASP.NET MVC?
Asked Answered
L

4

13

Currently, I'm using a strategy found on many blog posts. Basically, the URL contains the page number (e.g. /Users/List/5 will give you the users on page 5 of your paged list of users). However, I'm not running into a situation where one page must list two separate paged lists. How do I go about doing this using ASP.NET MVC? Do I simply provide two url parameters (e.g. /Users/List?page1=1&page2=2)? Is there a better way by using partial views?

Lauber answered 15/11, 2008 at 0:41 Comment(0)
B
10

Ignoring routes for just a minute, you'll just keep the state of the 2 different pages in the URL as querystring parameters.

mysite.com/foo?list1page=2&list2page=8

Then you build the data/pagers accordingly. The pager links will just update the url.

You can get creative with routes to create more friendly URLs, but I think querystring params are perfectly acceptable here.

Brougham answered 15/11, 2008 at 6:57 Comment(2)
What if you also have to keep up with multiple search parameters as well?Hembree
This becomes slightly more complex. You'll probably want to have a helper take in some context and generate the appropriate URL.Brougham
P
3

I've found ASP.NET MvcPager on the official asp.net site.

Paderewski answered 6/4, 2010 at 16:52 Comment(0)
B
2

If you use query parameters then the page links in each list have to know about each other in-order to pass both parameters.

If the two lists are independent of each other then you can update them individually using jquery & ajax. Simply place each list in their own div as a partial view.

<div id="list1"><%= Html.RenderPartial ("ListA") %></div>
<div id="list2"><%= Html.RenderPartial ("ListB") %></div>

Lookup ajaxSubmit from the jQuery Form plugin for an easy way to update the lists.

Blume answered 15/11, 2008 at 8:8 Comment(0)
C
1

PagedList.Mvc is a really easy to use plugin, and is available via Nuget too.

Coparcener answered 26/7, 2013 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.