在这一篇文章中,我们将进修如安在MVC页面中完成分页的要领。分页功用是一个异常有用,经常运用的功用,当数据量过量的时刻,必定要运用分页。在本日这篇文章中,我们进修假如在MVC页面中运用PagedList.Mvc包来完成分页功用。
1) 装置PagedList.Mvc
起首,我们须要装置分页组件包,在Visual Studio 2010中点击【项目】-【治理NuGet程序包】,翻开NuGet包治理器窗体,在该窗体中,挑选“联机”标签,然后搜刮pagedlist,以下图所示。点击“装置”按钮装置PagedList.Mvc的最新版本(如今最新版本为4.5.0)。
在把PagedList.Mvc装置完成以后,PagedList包也被装置上了。以下图。
图1:NuGet包治理器中显现的PagedList.Mvc
2) 完成带分页功用的视图实体对象和控制器
把PagedList.Mvc装置完成以后,第一件事就是增添一个视图实体对象,用来安排一些查询属性与查询结果。在Models目录下新增一个ViewBook.cs文件,代码如以下所示:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using PagedList; namespace MvcApplication1.Models { public class ViewBook { public IPagedList<Book> Books { get; set; } public string Search { get; set; } public string Category { get; set; } public string SortBy { get; set; } } }
我们如今须要修正BookController类的SearchIndex要领,以便Books作为PagedList返回(运用ToPagedList()要领完成)。为了运用PagedList,我们还须要设置默许排序。为了运用PagedList包,我们起首须要在该文件的顶部增加using PagedList;代码,然后修正Controllers\BookController.cs文件为以下粗体显现的代码。
public ActionResult SearchIndex(string Category, string searchString, string sortBy,int? page) { var cateLst = new List<string>(); var cateQry = from d in db.Books orderby d.Category select d.Category; cateLst.AddRange(cateQry.Distinct()); ViewBag.category = new SelectList(cateLst); //排序选项 var orderbyLst = new Dictionary<string, string> { { "价钱从低到高", "price_lowest" }, { "价钱从高到低", "price_highest" } }; ViewBag.sortBy = new SelectList(orderbyLst, "Value", "Key"); // [2017-2-14 end] var books = from m in db.Books select m; if (!String.IsNullOrEmpty(searchString)) { books = books.Where(s => s.Name.Contains(searchString)); } // sort the results switch (sortBy) { case "price_lowest": books = books.OrderBy(p => p.Price); break; case "price_highest": books = books.OrderByDescending(p => p.Price); break; default: books = books.OrderBy(p => p.Name); break; } //分页 const int pageItems = 5; int currentPage = (page ?? 1); IPagedList<Book> pageBooks = books.ToPagedList(currentPage, pageItems); // [2017-2-14] ViewBook vbook = new ViewBook(); vbook.Books = pageBooks; vbook.Category = Category; vbook.SortBy = sortBy; vbook.Search = searchString; if (string.IsNullOrEmpty(Category)) vbook.Books =pageBooks; else { vbook.Books =pageBooks.Where(x => x.Category == Category).ToPagedList(currentPage, pageItems); } return View(vbook); }
以上代码举行了以下频频发起,第一处修改是增加了一个int? page参数,它是一个可空整型,示意用户在书本查询页面中挑选确当前页码。当第一次加载书本查询页面时,用户还没有挑选任何页码,因而,这个参数能够为null。
我们必需确保当前的分类也要保留在视图实体对象中,因而,我们增加了vbook.Category = Category;这行代码。
代码books = books.OrderBy(p => p.Name);用于对产物列表举行默许排序,这是由于PagedList请求列表必需是一个有序列表。
接着,我们运用代码const int pageItems = 5;来指定每页显现的数据数目。然后,我们声清楚明了一个整型变量int currentPage = (page ?? 1);来保留当前页码,该变量的值是page参数的值,或许是1(当page变量为null时)。
我们运用代码vbook.Books = books.ToPagedList(currentPage, PageItems);,对产物信息调用了ToPagedList要领,并将当前页和每页显现的条目数传递给了ToPagedList要领,然后将该要领的返回值赋值给了视图实体对象的Books属性。
我们运用代码viewBook.SortBy = sortBy;将sortBy参数的值保留到视图实体对象的SortBy属性中,以便我们从一页挪动到另一页时,产物的排序坚持稳定。
3) 带分页功用的查询页面
在视图实体对象和控制器中对完成分页功用的代码举行修正以后,如今,我们须要更新视图文件\Views\Products\SearchIndex.cshtml,在这个视图文件中显现一个分页控件,以便用户能够在各页之间挪动。我们同时也增加了有若干条数据的指导信息。为了完成这些功用,我们在该文件中增加了一个using语句,一个书本总数的指导信息以及在该页底部显现一个分页控件,详细代码以下面显现:
@model MvcApplication1.Models.ViewBook @using PagedList.Mvc @{ ViewBag.Title = "书本查询"; } <link href="/Content/PagedList.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" /> <h2>书本查询</h2> @using (Html.BeginForm("SearchIndex","book",FormMethod.Get)){ <p>书本品种: @Html.DropDownList("category", "All") 书本称号: @Html.TextBox("SearchString") 排序: @Html.DropDownList("sortBy", "不排序") <input type="submit" value="查询" /> </p> } <table> <tr> <th> @Html.DisplayNameFor(model => model.Books.First().Category) </th> <th> @Html.DisplayNameFor(model => model.Books.First().Name) </th> <th> @Html.DisplayNameFor(model => model.Books.First().Numberofcopies) </th> <th> @Html.DisplayNameFor(model => model.Books.First().AuthorID) </th> <th> @Html.DisplayNameFor(model => model.Books.First().Price) </th> <th> @Html.DisplayNameFor(model => model.Books.First().PublishDate) </th> <th></th> </tr> @foreach (var item in Model.Books) { <tr> <td> @Html.DisplayFor(modelItem => item.Category) </td> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Numberofcopies) </td> <td> @Html.DisplayFor(modelItem => item.AuthorID) </td> <td> @Html.DisplayFor(modelItem => item.Price) </td> <td> @Html.DisplayFor(modelItem => item.PublishDate) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.BookID }) | @Html.ActionLink("Details", "Details", new { id=item.BookID }) | @Html.ActionLink("Delete", "Delete", new { id=item.BookID }) </td> </tr> } </table> <p> Page @(Model.Books.PageCount < Model.Books.PageNumber ? 0 : Model.Books.PageNumber) of @Model.Books.PageCount @Html.PagedListPager(Model.Books, page => Url.Action("SearchIndex", new { category = Model.Category, search = Model.Search, sortBy = Model.SortBy, page })) </p>
分页链接生成代码包裹在p标签内。个中第一行代码运用?:操作符的第一行代码决议是不是有任何页码显现,它显现“Page 0 of 0”或许“Page x of y”,x示意当前页码,y示意总页数。
第二行代码运用来自于PagedList.Mvc定名空间的PagedListPager辅佐器。该辅佐器吸收一个产物列表参数,并为每一个页面生成一个超链接。Url.Action用于生成一个含有当前页参数超链接目的。我们将一个匿名范例(含有当前分类、搜刮前提、排序信息和分页)传递给该辅佐器要领,以便每一个页面的链接中都包括一个查询字符串,这个查询字符串包括有当前分类、搜刮前提、排序信息和分页信息。这意味着,当从一个页面挪动到另一个页面时,搜刮前提、挑选的分类和排序划定规矩都被保留下来。假如没有如许做,书本列表将会被重置为显现一切书本信息。
在运用了上述代码后,按“价钱从低到高”排序分页界面,以下图1。
图1
我们发明分页的数字部份,并不悦目,本来我们缺乏引用了CSS,在查询页面的题目下方增加以下代码。在上述代码中的蓝色字体。
<link href="/Content/PagedList.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
再次点击“查询”按钮,然后对其结果根据“价钱从低到高”举行排序,结果以下图2。
图2:有搜刮前提、排序和按分类过滤的分页结果
以上就是运用ASP.NET MVC分页的完成要领引见的细致内容,更多请关注ki4网别的相干文章!