标签:
估计是因为水平原因,之前看别人写的用pagedList分页,老是云里雾里的。下面把自己写的整理一下放在上面。这里的List为对应页面展示的内容。不用查询所有。
Action:
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder??"sortOrder";
ViewBag.CurrentFilter = currentFilter ?? "currentFilter";
List<Student> students = new List<Student>();
for (int i = 0; i < 10; i++)
{
students.Add(
new Student()
{
SchoolId = i,
Name = "Name" + i,
Grad = "Grad" + i,
});
}
//public StaticPagedList(IEnumerable<T> subset, int pageNumber, int pageSize, int totalItemCount);
int pageNumber = (page ?? 1);
var firstPage = new StaticPagedList<Student>(students, pageNumber, 10, 100);
return View(firstPage);
}
view:
@model PagedList.IPagedList<PageListDemo.ModelDisplay.Student>
@using PagedList.Mvc; <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{ ViewBag.Title = "主页"; }
<table class="table">
<tr>
<th>StudentId</th>
<th>Name</th>
<th>Grad </th>
<th></th> </tr>
@foreach (var item in Model) {
<tr> <td> @Html.DisplayFor(modelItem => item.SchoolId) </td> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Grad) </td> </tr> } </table> <br /> Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount <br /> Results:@Model.TotalItemCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }), new PagedListRenderOptions(){ MaximumPageNumbersToDisplay=5, DisplayLinkToIndividualPages=true, LinkToFirstPageFormat="首页", LinkToLastPageFormat="尾页", LinkToNextPageFormat="下一页", LinkToPreviousPageFormat="上一页", DisplayEllipsesWhenNotShowingAllPageNumbers=false, })
标签:
原文地址:http://www.cnblogs.com/beagreatman/p/4458651.html