To use PagedList in .Net Core 3.0 we will use https://github.com/dncuug/X.PagedList
Open NuGet Package Manager Install X.PagedList.Mvc.Core
_ViewImports.cshtml
@using X.PagedList.Mvc.Core;
@using X.PagedList;
@using X.PagedList.Mvc.Common
BrandController.cs
public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
var pageSize = 10; //Show 10 rows every time
var brands = this._UoW.BrandsRepository.GetAll().ToPagedList(pageNumber,pageSize);
return View(brands);
}
Index.cshtml
Change
@model IEnumerable<Stock.Data.Models.Brands>
to
@model IPagedList<Stock.Data.Models.Brands>
Change every
@Html.DisplayNameFor(model => model.BrandName)
To
@Html.DisplayNameFor(model => model.First().BrandName)
At the the end of your html table add paging numbers like
<div class="pull-right">
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index",
new
{
page
}),
new PagedListRenderOptionsBase
{
LiElementClasses = new string[] { "page-item" },
PageClasses = new string[] { "page-link" },
Display = PagedListDisplayMode.IfNeeded
})
</div>
If you make search or using other querystring you have to do something like that:
BrandController.cs
public ActionResult Index(string search,int? page)
{
var pageNumber = page ?? 1;
var pageSize = 10; //Show 10 rows every time
var brands = this._UoW.BrandsRepository.GetAll().Where(b =>
b.BrandName.Contains(search) ||
search == null).ToPagedList(pageNumber, pageSize);
return View(brands);
}
Index.cshtml
At the the end of your html table add paging numbers like
<div class="pull-right">
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index",
new
{
page,
search = Context.Request.Query["search"]
}),
new PagedListRenderOptionsBase
{
LiElementClasses = new string[] { "page-item" },
PageClasses = new string[] { "page-link" },
Display = PagedListDisplayMode.IfNeeded
})
</div>
For best performance use
.ToPagedList
with IQueryable
so you will return only 10 rows from the database every time not the whole rows
Use
public IQueryable<T> GetAll()
{
return this._DbSet;
}
with
this._UoW.BrandsRepository.GetAll().ToPagedList(pageNumber,pageSize);
Do not use .ToList()
this._UoW.BrandsRepository.GetAll().ToList().ToPagedList(pageNumber,pageSize);
Do not use
public IEnumerable<T> GetAll()
{
return this._DbSet;
}
with
this._UoW.BrandsRepository.GetAll().ToPagedList(pageNumber,pageSize);
.Core
in the name, it doesn't mean its for .NET Core! "Core" in package names (even before .NET Core was there) is a different word for "Common" or "Shared". A bunch denominator which acts as base package for other packages. Packages for ASP.NET Core usually have the identifier "AspNetCore" in their package name. And libraries in general for .NET Core just have the same names as ones for .NET Framework, but targetnetstandard
(you can see that in dependencies section on NuGet)