C#MVC中分页操作及控件使用方法

一、打开nuget搜索pagedList.mvc控件,并安装

二、在controller

using PagedList;

public ActionResult Index(int? page)

{

//...

//第几页
int pageNumber = page ?? 1;
//每页显示多少条
int pageSize = int.Parse(ConfigurationManager.AppSettings["pageSize"]);
//通过ToPagedList扩展方法进行分页
IPagedList<Schedule> pagedList = schedule.ToPagedList(pageNumber, pageSize);
return View(pagedList);

}

三、在View中添加

@using PagedList;
@using PagedList.Mvc;

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

//@Html.PagedListPager(Model as IPagedList, n=> Url.Action("Index", new { page=n }))

 

四、如果带有额外参数进行分页

将使用ViewBag来传递额外参数

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

声明:本站内容来源于原创和互联网,尊重作者版权,转载请注明来源网址,欢迎收藏,谢谢!