好像是PagedList.Core
不包含 Html helper 的扩展方法,所以我不能使用下面的代码:
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }), PagedListRenderOptions.MinimalWithItemCountText)
我能够在以前版本的 MVC 中成功实现分页,但它在 ASP.Net Core 中不起作用。下面我附上了 IL Dasm 引用。我错过了什么,还是有其他方法可以实现?
分页列表.Mvc:
PagedList.Core.Mvc:
最佳答案
要在 .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>
为获得最佳性能,请使用
.ToPagedList
与 IQueryable
所以你每次只会从数据库中返回 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/