最近的项目中用到了MVC的分页
因此,自己就写了一个简单实用的第三方分页用来和大家分享!
例图:
在使用分页前,先添加引用:
点开vs上方的工具,在点NuGet包管理器中的程序包管理器控制台
输入 Install-Package PagedList.mvc 回车及可用
添加好后Content文件夹中会多一个
<link href="~/Content/PagedList.css" rel="stylesheet" />
控制器 Controler
using PagedList; //引用命名空间
public ActionResult ShowJt(int pageIndex=1)
{
//接收跨域的数据 或 无跨域从三层bll传递来的数据
string result = HttpClientHelper.Send("get", "api/jia", null);
List<JiaViewModel> jias = JsonConvert.DeserializeObject<List<JiaViewModel>>(result);
//每页显示5条 pageIndex为当页索引
int pageSize = 4;
//总记录数
int toTalCount = jias.Count; //总记录数
IPagedList<JiaViewModel> pagedList = jias.ToPagedList(pageIndex, pageSize);
return View(pagedList);
}
视图 view
@using PagedList.Mvc;
@model PagedList.IPagedList<ShenPiMvc.Models.JiaViewModel>
<link href="~/Content/PagedList.css" rel="stylesheet" />
//写好查询内容之后 直接加在下方即可
每页 @Model.PageSize 条记录,共 @Model.PageCount 页,当前第 @Model.PageNumber 页
@Html.PagedListPager(Model,pageIndex=>Url.Action("ShowJt",new { pageIndex }),
new PagedListRenderOptions
{
LinkToFirstPageFormat="首页",
LinkToNextPageFormat="下一页",
LinkToPreviousPageFormat="上一页",
LinkToLastPageFormat="末页",
MaximumPageNumbersToDisplay=0,
DisplayItemSliceAndTotal=false
}
)
到这里关于IPagedList第三方分页的讲解也就结束了,相信大家也都学会了。
原文:https://www.cnblogs.com/xuan666/p/10725436.html