model-view-controller - .Net Core 中的 PagedList.Core.Mvc PagedListPager Html 扩展不存在

标签 model-view-controller asp.net-core asp.net-core-mvc pagedlist

好像是PagedList.Core不包含 Html helper 的扩展方法,所以我不能使用下面的代码:

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

我能够在以前版本的 MVC 中成功实现分页,但它在 ASP.Net Core 中不起作用。下面我附上了 IL Dasm 引用。我错过了什么,还是有其他方法可以实现?

分页列表.Mvc:

enter image description here

PagedList.Core.Mvc:

enter image description here

最佳答案

要在 .Net Core 3.0 中使用 PagedList,我们将使用 https://github.com/dncuug/X.PagedList

打开 NuGet 包管理器 安装 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

改变
@model IEnumerable<Stock.Data.Models.Brands>


@model IPagedList<Stock.Data.Models.Brands>

改变每
@Html.DisplayNameFor(model => model.BrandName)


@Html.DisplayNameFor(model => model.First().BrandName)

在你的 html 表格的末尾添加分页号码,如
<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>

如果您进行搜索或使用其他查询字符串,则必须执行以下操作:

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
在你的 html 表格的末尾添加分页号码,如
            <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>


为获得最佳性能,请使用
.ToPagedListIQueryable
所以你每次只会从数据库中返回 10 行而不是整行

使用
        public IQueryable<T> GetAll()
        {
            return this._DbSet;
        }


this._UoW.BrandsRepository.GetAll().ToPagedList(pageNumber,pageSize);

请勿使用 .ToList()
this._UoW.BrandsRepository.GetAll().ToList().ToPagedList(pageNumber,pageSize);

请勿使用
        public IEnumerable<T> GetAll()
        {
            return this._DbSet;
        }


this._UoW.BrandsRepository.GetAll().ToPagedList(pageNumber,pageSize);

关于model-view-controller - .Net Core 中的 PagedList.Core.Mvc PagedListPager Html 扩展不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41517359/

相关文章:

ios - MVC 中的智能行为(不是计算数据)? iOS

c# - 值不能为空。参数名称 : source when trying to get data with web api

azure - 在 Azure WebApp 中使用环境变量引用时如何解析 secret

razor - 获取脚手架以特定顺序生成字段

c# - ASP.NET Core 2.0 防止重复条目

c# - 先用点表示小数,再用逗号表示

javascript - 为什么我的过载 Controller 方法不能正确命中?

grails - 用外键插入Grails Domain对象

c# - 如何只验证 ASP .NET MVC 中模型的一部分?

asp.net - ASP.NET Core (ASP.NET 5) 应用程序中有哪些可用框架?