MVC分页之MvcPager使用详解

最近刚刚接触MVC不久,因项目中要用到分页,网上找了下资料,最后采用了MvcPager(),支持同步和Ajax异步分页。废话不多说了直接上代码。 

一.MvcPager异步
 ViewModel: 

public class Article { [Display(Name = "信息编号")] public int ID { get; set; } [Display(Name = "信息标题")] public string Title { get; set; } [Display(Name = "信息内容")] public string Content { get; set; } }

public class AjaxPager { public PagedList<Article> Articles { get; set; } }

Control:

/// <summary> /// 异步分页测试 /// </summary> /// <param>pageIndex</param> /// <param>关键字</param> /// <returns></returns> public ActionResult AjaxPaging(int? id = 1, string key = null) { int totalCount = 0; int pageIndex = id ?? 1; int pageSize = 2; List<Article> infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - 1) * 2, out totalCount); PagedList<Article> InfoPager = infoList.AsQueryable().OrderByDescending(o => o.ID).ToPagedList(pageIndex, pageSize); InfoPager.TotalItemCount = totalCount; InfoPager.CurrentPageIndex = (int)(id ?? 1); Models.MyTest.AjaxPager model = new Models.MyTest.AjaxPager(); model.Articles = InfoPager; if (Request.IsAjaxRequest()) { return PartialView("_ArticleList", model); } return View(model); }

View: 

@model soulefu_manage.Models.MyTest.AjaxPager @using Webdiyer.WebControls.Mvc; <!DOCTYPE html> <html> <head> <meta content="width=device-width" /> <title>MVCPager-AjaxPaging</title> <link href="https://www.jb51.net/~/Content/pagerstyles.css" /> <link href="https://www.jb51.net/~/Content/bootstrap.css" /> </head> <body> <div> @using (Html.BeginForm("AjaxPaging", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get)) { @Html.Label("关键字:") <input value="@Request.QueryString["key"]" /><input type="submit" value="查询" /> } @*分页Table*@ @{ Html.RenderPartial("_ArticleTable"); } <div> @Ajax.Pager(Model.Articles, new PagerOptions { PageIndexParameterName = "id", FirstPageText = "首页", PrevPageText = "上一页", NextPageText = "下一页", LastPageText = "末页", NumericPagerItemCount = 5, ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=https://www.jb51.net/article/\"active\"><a href=https://www.jb51.net/article/\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=https://www.jb51.net/article/\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>" }).AjaxOptions(a => a.SetUpdateTargetId("articles")) </div> </div> </body> </html>

@model soulefu_manage.Models.MyTest.AjaxPager <table> <tr> <th>序号</th> <th> 标题 </th> <th> 内容 </th> </tr> @foreach (var item in Model.Articles) { <tr> <td>@Html.DisplayFor(model => item.ID)</td> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.Content) </td> </tr> } </table>

二.MvcPager同步
  ViewModel(此处可不增加,直接和异步的共用同一个): 

public class MVCPager { //信息列表 public PagedList<Article> Articles { get; set; } }

Control: 

/// <summary> /// 同步分页测试 /// </summary> /// <param>pageIndex</param> /// <param>关键字</param> /// <returns></returns> public ActionResult MVCPager(int? id = 1, string key = null) { int totalCount = 0; int pageIndex = id ?? 1; int pageSize = 2; List<Article> infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - 1) * 2, out totalCount); PagedList<Article> InfoPager = infoList.AsQueryable().OrderByDescending(o => o.ID).ToPagedList(pageIndex, pageSize); InfoPager.TotalItemCount = totalCount; InfoPager.CurrentPageIndex = (int)(id ?? 1); //数据组装到viewModel Models.MyTest.MVCPager model = new Models.MyTest.MVCPager(); model.Articles = InfoPager; return View(model); }

View: 

@model soulefu_manage.Models.MyTest.MVCPager @using Webdiyer.WebControls.Mvc; <!DOCTYPE html> <html> <head> <meta content="width=device-width" /> <title>MVCPager</title> <link href="https://www.jb51.net/~/Content/pagerstyles.css" /> <link href="https://www.jb51.net/~/Content/bootstrap.css" /> </head> <body> <div> @using (Html.BeginForm("MVCPager", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get)) { @Html.Label("关键字:")<input value="@Request.QueryString["key"]" /><input type="submit" value="查询" /> } <table> <tr> <th>编号</th> <th>标题</th> <th>内容</th> </tr> @foreach (var info in Model.Articles) { <tr> <td>@Html.DisplayFor(model => info.ID)</td> <td>@Html.DisplayFor(model => info.Title)</td> <td>@Html.DisplayFor(model => info.Content)</td> </tr> } </table> <div> <nav> @Html.Pager(Model.Articles, new PagerOptions { PageIndexParameterName = "id", FirstPageText = "首页", PrevPageText = "上一页", NextPageText = "下一页", LastPageText = "末页", ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=https://www.jb51.net/article/\"active\"><a href=https://www.jb51.net/article/\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=https://www.jb51.net/article/\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>", Id = "bootstrappager" }) </nav> </div> </div> </body> </html>

获取测试数据方法(共用):

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

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