使用X.PagedList.Mvc 分页(ASP.NET Core 3.1)
直接上干货
首先Nuget包添加引用: X.PagedList.Mvc.Core
控制器:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Dw.BLL.Article; using Dw.Models.Article; using Dw.Models.Other; using Microsoft.AspNetCore.Mvc; using X.PagedList; namespace Dw.Histolab.Controllers { public class ArticleController : Controller { private Article_CategoryBLL article_CategoryBLL = new Article_CategoryBLL(); private Article_ContentBLL article_ContentBLL = new Article_ContentBLL(); #region 产品列表 /// <summary> /// 产品列表 /// </summary> /// <param name="keyValue">类别主键</param> /// <param name="page">第几页</param> /// <param name="pageSize">每页记录数</param> /// <returns></returns> public IActionResult ProductList(string keyValue, int page = 1, int pageSize = 12) { var articleCategory = article_CategoryBLL.GetEntity(keyValue); ViewBag.CategoryName = articleCategory.Name; ViewBag.CategoryId = articleCategory.CategoryId; var articleContentList = article_ContentBLL.GetList(keyValue); return View(articleContentList.ToPagedList(page, pageSize)); } #endregion } }
视图页
@model X.PagedList.IPagedList<Dw.Models.Article.Article_Content> @using X.PagedList.Mvc.Core; @{ ViewData["Title"] = "产品中心列表"; } <link href="~/css/PagedList.css" rel="stylesheet" /> <div id="contentWrapper"> <div id="contentWrap"> <div id="content"> <div id="prdBrand"> <div class="bcate">产品类别:@ViewBag.CategoryName</div> <div class="item-wrap"> <div class="item-cont"> @foreach (var item in Model) { <dl class="item-list"> <div class="item-icon"> <span class='MK-product-icons'> <img src='~/picture/2903.jpg' class='MK-product-icon-2'> </span> </div> <dt class="thumb"> <a href="shopdetail.html"> <img class="MS_prod_img_m" src="@item.PictureUrl" alt="@item.Title" title="@item.Title"> </a> </dt> <dd> <ul> <li class="prd-name">@item.Title</li> <li class="prd-subname">@item.Summary</li> </ul> </dd> </dl> } </div> <div style="text-align:center"> 每页 @Model.PageSize 条记录,本页有 @Model.Count 条记录,共有 @Model.TotalItemCount 条记录。第 @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) 页,共 @Model.PageCount 页。 @Html.PagedListPager(Model, page => Url.Action("ProductList", new { page ,keyValue=ViewBag.CategoryId})) </div> </div> </div> </div> <hr> </div> </div>