Welcome to our blog Contact Us Subscribe!

How to Add Pagination in Asp.net Core MVC

Here, In this blog post, I have shared you the solution for how to add pagination in asp.net core mvc. This can be implemented in every dotnet version
Anonymous

How to Add Pagination in Asp.net Core MVC

Here, In this blog post, I have shared you the solution for how to add pagination in asp.net core mvc. This can be implemented in every dotnet version.

How to Add Pagination in Asp.net Core MVC

Steps to Add Pagination in Asp.net Core MVC

1.  Install Nuget Package: X.PagedList.Mvc.Core

2. Add paging functionality to the Index method

public async Task<IActionResult> Index(int? page)
{
	var listOfPostsVM = new List<PostViewModel>();

	listOfPostsVM = _context.Posts!.ToList();

	int pageSize = 5;
	int pageNumber = (page ?? 1);
	return View(await listOfPostsVM.ToPagedListAsync(pageNumber,pageSize));
}

3. Add paging links to the Student index view


@model IPagedList<PostViewModel>
@using X.PagedList
@using X.PagedList.Mvc.Core
@using X.PagedList.Web.Common

//Content
//Content
//Content

Page @(Model?.PageCount < Model?.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model,page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }),
new PagedListRenderOptions()
{
  ContainerDivClasses = new List<string> {"blog","navigation"},
  UlElementClasses = new List<string> {"pagination"},
  LiElementClasses = new List<string> {"page-item","page-link"},
  ActiveLiElementClass =  "active",
})

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.