How to add pagination to asp.net

One of the most important feature on a website that has a search feature is that it allows for pagination (allow you to page the results). The reason you would want to make pages is to make your website run faster for the user. Rather than displaying 1000 results on one page you can change it to display 10 results on one each page with a total of 100 pages, which speeds up the site and makes navigation easier.

You will need to install the nuget package PagedList.Mvc. This will automatically install the package PagedList as well.

In my example I am working with a search feature, where the user will enter a string in the textbox and the Index controller will query the Documents  database for a matching Title.

In the controller we will need to pass another parameter int page, which is the number of pages and assign it to 1 as a default value. Then in the linq query we need to add ToPagedList(page, 10). The page being the number of pages, and 10 is the number of results per page.

Search Controller:

In the view rather than using @model IEnumerable<Document> you would use @model IPagedList<Document>, because in the linq query we convert it to ToPagedList.

View:

Bookmark and Share

Leave a Reply

Your email address will not be published. Required fields are marked *