MVC5 + EF6 + Bootstrap3 (11) 实现排序、搜索、分页(2)

之后会在Visual Studio窗口的下面看到一个程序包管理控制台窗口。在其中输入Install-Package PagedList.Mvc。PagedList就会自动安装到项目中。窗口如下所示。

MVC5 + EF6 + Bootstrap3 (11) 实现排序、搜索、分页

Paged List 安装好后,我们就开始修改代码,从Controller开始:

using System.Linq; using System.Web.Mvc; using SlarkInc.DAL; using System; using PagedList; namespace SlarkInc.Controllers { public class CompanyController : Controller { private CompanyContext db = new CompanyContext(); public ViewResult Index(string sortOrder, string searchString, string currentFilter, int? page) { ViewBag.CurrentSort = sortOrder; ViewBag.FirstNameSortParm = String.IsNullOrEmpty(sortOrder) ? "first_desc" : ""; ViewBag.LastNameSortParm = sortOrder == "last" ? "last_desc" : "last"; if(searchString != null) { page = 1; } else { searchString = currentFilter; } ViewBag.CurrentFilter = searchString; var workers = from w in db.Workers select w; if (!string.IsNullOrEmpty(searchString)) { workers = workers.Where(w => w.FirstName.Contains(searchString) || w.LastName.Contains(searchString)); } switch (sortOrder) { case "first_desc": workers = workers.OrderByDescending(w => w.FirstName); break; case "last_desc": workers = workers.OrderByDescending(w => w.LastName); break; case "last": workers = workers.OrderBy(w => w.LastName); break; default: workers = workers.OrderBy(w => w.FirstName); break; } int pageSize = 3; int pageNumber = (page ?? 1); return View(workers.ToPagedList(pageNumber,pageSize)); } } }

黄色标记的为需要改动的部分。第5行,需要引用PagedList。第12行currentFilter参数用来在翻页时保持搜索字符串不丢失。第17到25行的作用是,当有一个新的字符串要搜索,那么翻当前页就自动变成第一页,否则,当前页不变。第49-51行设置每页3条数据,设置页数,并将数据以List的形式发送个View。其中(page ?? 1)的意思是如果page为null则给page赋值为1否则,page不为null那么该是多少就是多少。所以能让默认页为1.

@model PagedList.IPagedList<SlarkInc.Models.Worker> @using PagedList.Mvc; <link href="https://www.jb51.net/~/Content/PagedList.css" /> <br /> @using(Html.BeginForm("Index","Company",FormMethod.Get)) { <p> Find by name: @Html.TextBox("SearchString",ViewBag.CurrentFilter as string, new { @class = "form-control", @Style = "display:inline" }) @Html.Submit("Submit") </p> } <table> <tr> <th> @Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.FirstNameSortParm }) </th> <th> @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.LastNameSortParm }) </th> <th>Sex</th> <th>Rating</th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.FirstName) </td> <td> @Html.DisplayFor(modelItem => item.LastName) </td> <td> @Html.DisplayFor(modelItem => item.Sex) </td> <td> @Html.DisplayFor(modelItem => item.Rating) </td> </tr> } </table> <br/> Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount @Html.PagedListPager(Model, page => Url.Action("Index", new {page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter}))

第1行定义了PagedList 类型的model。第2行引用PagedList。第3行,引入,PagedList相关的css文件,这个文件在安装插件的时候会自动保存到项目里。第5行BeginForm函数的参数有变化,其生成的Form会使用Get方法。Get方法会在url里显示提交的参数,这样可以通过url来记录查询参数,方便通过复制url给他人来展现自己的查询结果。第42行用于显示第几页,共几页。第43-44行,用于生成每一页的按钮。

运行此页面,当页面出来时点击页面下方的"2"按钮转到第2页。生成的页面如下所示:

MVC5 + EF6 + Bootstrap3 (11) 实现排序、搜索、分页

可以看到页面下方的分页按钮和页数统计,美观大方。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjywdg.html