luc*_*cas 4 model-view-controller pagedlist asp.net-core-mvc asp.net-core
好像PagedList.Core不包含Html帮助器的扩展方法,所以我不能使用下面的代码:
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }), PagedListRenderOptions.MinimalWithItemCountText)
Run Code Online (Sandbox Code Playgroud)
我能够在以前版本的MVC中成功实现分页,但它在ASP.Net Core中不起作用.下面我附上了IL Dasm参考.我错过了什么,或者还有其他方法可以实现吗?
PagedList.Mvc:
PagedList.Core.Mvc:
小智 7
要在 .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
Run Code Online (Sandbox Code Playgroud)
品牌控制器.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);
}
Run Code Online (Sandbox Code Playgroud)
索引.cshtml
改变
@model IEnumerable<Stock.Data.Models.Brands>
Run Code Online (Sandbox Code Playgroud)
到
@model IPagedList<Stock.Data.Models.Brands>
Run Code Online (Sandbox Code Playgroud)
改变每
@Html.DisplayNameFor(model => model.BrandName)
Run Code Online (Sandbox Code Playgroud)
到
@Html.DisplayNameFor(model => model.First().BrandName)
Run Code Online (Sandbox Code Playgroud)
在你的 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>
Run Code Online (Sandbox Code Playgroud)
如果您进行搜索或使用其他查询字符串,则必须执行以下操作:
品牌控制器.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);
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
为获得最佳性能使用
.ToPagedList 和 IQueryable
所以你每次只会从数据库中返回 10 行而不是整行
用
public IQueryable<T> GetAll()
{
return this._DbSet;
}
Run Code Online (Sandbox Code Playgroud)
和
this._UoW.BrandsRepository.GetAll().ToPagedList(pageNumber,pageSize);
Run Code Online (Sandbox Code Playgroud)
不要使用.ToList()
this._UoW.BrandsRepository.GetAll().ToList().ToPagedList(pageNumber,pageSize);
Run Code Online (Sandbox Code Playgroud)
不使用
public IEnumerable<T> GetAll()
{
return this._DbSet;
}
Run Code Online (Sandbox Code Playgroud)
和
this._UoW.BrandsRepository.GetAll().ToPagedList(pageNumber,pageSize);
Run Code Online (Sandbox Code Playgroud)
我应该遵循新版本的说明,它与以前的版本有点不同.我能够使用以下代码更改实现分页:
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
Run Code Online (Sandbox Code Playgroud)
_ViewImports.cshtml:
@addTagHelper *, PagedList.Core.Mvc
Run Code Online (Sandbox Code Playgroud)
最后使用它,我们不再在.Net Core中使用Html标记助手了:
<pager class="pager-container" list="@Model" options="@PagedListRenderOptions.TwitterBootstrapPager" asp-action="Index" asp-controller="ControllerName" />
Run Code Online (Sandbox Code Playgroud)
小智 6
X.PagedList.Mvc.Core
在您的项目中包含nuget。然后将其添加到您的视图中:
@using X.PagedList.Mvc.Core
Run Code Online (Sandbox Code Playgroud)
那么这应该工作:
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5951 次 |
| 最近记录: |