Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you implement paging in ASP.NET MVC?

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?

like image 211
Kevin Pang Avatar asked Nov 15 '08 00:11

Kevin Pang


People also ask

How can we implement pagination in asp net core?

How to implement paging in ASP.NET Core Web API. In an empty project, update the Startup class to add services and middleware for MVC. Add models to hold link and paging data. Create a type to hold the paged list.

What is paging in asp net?

This is what custom paging is all about i.e. fetching only required data from database. To enable Custom paging we need to explicitly set AllowCustomPaging property of DataGrid control to True. Use PageSize property to set the number of records to be displayed per page.

What is paging in C#?

The C# pagination logic is contained in a single Pager class that takes the following constructor arguments: totalItems (required) - the total number of items to be paged. currentPage (optional) - the current active page, defaults to the first page. pageSize (optional) - the number of items per page, defaults to 10.


Video Answer


2 Answers

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.

like image 120
Ben Scheirman Avatar answered Oct 07 '22 20:10

Ben Scheirman


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

like image 38
nick4eva Avatar answered Oct 07 '22 19:10

nick4eva