11

It seems like the PagedList.Core does not contain the extension method for Html helper, so I cannot use the code below:

@Html.PagedListPager(Model, page => Url.Action("Index", new { page }), PagedListRenderOptions.MinimalWithItemCountText)

I was able to successfully implement the paging in previous version of MVC, but it does not work in ASP.Net Core. Below I have attached IL Dasm reference. Am I missing something, or is there any other way to implement that?

PagedList.Mvc:

enter image description here

PagedList.Core.Mvc:

enter image description here

4
  • Which package are you using? (NuGet Link would be usefull). Just because something has .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 target netstandard (you can see that in dependencies section on NuGet)
    – Tseng
    Jan 7, 2017 at 12:37
  • Examples for that are nuget.org/packages/Sakura.AspNetCore.PagedList, it has a ".NETStandard 1.0" section
    – Tseng
    Jan 7, 2017 at 12:45
  • The Nuget Package name is PagedList.Core.Mvc by Troy Goode. This one is dedicated for .Net Core, his previous version mentioned above works just fine in previous version of the framework
    – lucas
    Jan 7, 2017 at 17:41
  • Here is the NuGet reference: nuget.org/packages/PagedList.Core.Mvc
    – lucas
    Jan 7, 2017 at 17:48

3 Answers 3

10

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);
0
7

I should follow the Instructions of the new version, it is a little different from previous version. I was able to implement the paging with the following code changes:

Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
    }

_ViewImports.cshtml:

@addTagHelper *, PagedList.Core.Mvc

And finally to use it, we don't use Html tag helper anymore in .Net Core:

<pager class="pager-container" list="@Model" options="@PagedListRenderOptions.TwitterBootstrapPager" asp-action="Index" asp-controller="ControllerName" />
4
  • Helpful, but could you link to the docs? The NuGet packages points to the old ProjectUrl, I can't locate any page the new one.
    – H H
    Apr 15, 2017 at 18:07
  • What would I add here so that my request for the next page is directed as a Post request and not as a Get request.
    – Umar Aftab
    Oct 12, 2017 at 20:56
  • its worked for me ... but no page numbers to navigate at the botton Apr 23, 2018 at 10:50
  • I like this solution using the Microsoft Package over using the x pagedlist. Couple things to note. If you brought over the model from .net into .net core, the list will be the @Model.SearchResults, and as far as rendering options i like using the page numbers only so i use the options="@PagedListRenderOptions.Bootstrap4PageNumbersOnly" instead of the TwitterBootstrapPager
    – Mike
    Nov 26, 2021 at 23:09
6

Include X.PagedList.Mvc.Core nuget in your project. Then add this to your view:

@using X.PagedList.Mvc.Core

Then this should work:

@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.