• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

MVC分页之MvcPager使用详解

asp 搞代码 4年前 (2022-01-03) 14次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了MVC分页之MvcPager使用方法,针对MvcPager同步和Ajax异步分页进行讲解,感兴趣的小伙伴们可以参考一下

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

一.MvcPager异步
 ViewModel: 

 public class Article { [Display(Name = "信息编号")] public int ID { get; set; } [Display(Name =<p style="color:transparent">来源gao!daima.com搞$代!码网</p> "信息标题")] public string Title { get; set; } [Display(Name = "信息内容")] public string Content { get; set; } } 
 public class AjaxPager { public PagedList Articles { get; set; } } 

Control:

 /// <summary> /// 异步分页测试 /// </summary> /// pageIndex /// 关键字 ///  public ActionResult AjaxPaging(int? id = 1, string key = null) { int totalCount = 0; int pageIndex = id ?? 1; int pageSize = 2; List infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - 1) * 2, out totalCount); PagedList 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;   <title>MVCPager-AjaxPaging</title> <div style="padding: 15px"> @using (Html.BeginForm("AjaxPaging", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get)) { @Html.Label("关键字:")  } @*分页Table*@ @{ Html.RenderPartial("_ArticleTable"); } <div class="text-center"> @Ajax.Pager(Model.Articles, new PagerOptions { PageIndexParameterName = "id", FirstPageText = "首页", PrevPageText = "上一页", NextPageText = "下一页", LastPageText = "末页", NumericPagerItemCount = 5, ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class="active">{0}</li>", DisabledPagerItemTemplate = "<li class="disabled">{0}</li>", PagerItemTemplate = "<li>{0}</li>" }).AjaxOptions(a => a.SetUpdateTargetId("articles")) </div></div>
 @model soulefu_manage.Models.MyTest.AjaxPager <table class="table table-bordered table-striped"> <tr> <th class="nowrap">序号</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 Articles { get; set; } } 

Control: 

 /// <summary> /// 同步分页测试 /// </summary> /// pageIndex /// 关键字 ///  public ActionResult MVCPager(int? id = 1, string key = null) { int totalCount = 0; int pageIndex = id ?? 1; int pageSize = 2; List infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - 1) * 2, out totalCount); PagedList 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;   <title>MVCPager</title> <div style="padding:15px"> @using (Html.BeginForm("MVCPager", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get)) { @Html.Label("关键字:") } <table class="table table-bordered table-striped"> <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 class="text-center"> <nav> @Html.Pager(Model.Articles, new PagerOptions { PageIndexParameterName = "id", FirstPageText = "首页", PrevPageText = "上一页", NextPageText = "下一页", LastPageText = "末页", ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class="active">{0}</li>", DisabledPagerItemTemplate = "<li class="disabled">{0}</li>", PagerItemTemplate = "<li>{0}</li>", Id = "bootstrappager" }) </nav></div></div>

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

 public class MyTest { /// <summary> /// 获取测试数据 /// </summary> ///  ///  ///  ///  ///  public List GetArticleList(string key, int PageSize, int CurrentCount, out int TotalCount) { string tabName = string.Format("Article"); string strWhere = " 1=1"; if (!string.IsNullOrEmpty(key)) { //SQL关键字过滤 包含关键字则不拼接SQL if (!SqlInjection.GetString(key)) { strWhere += string.Format(" AND (Title LIKE '%{0}%' OR Content LIKE '%{0}%')", key); } } string Order = string.Format("ID ASC"); DataSet ds = SqlHelper.GetList(SqlHelper.connStr, Order, PageSize, CurrentCount, tabName, strWhere, out TotalCount); List list = new List(); if (ds != null && ds.Tables.Count > 0) { foreach (DataRow dr in ds.Tables[0].Rows) { Article model = new Article(); model.ID = Convert.ToInt32(dr["ID"]); model.Title = dr["Title"].ToString(); model.Content = dr["Content"].ToString(); list.Add(model); } } return list; } } 

效果图:(需要引用CSS)

以上就是MVC分页之MvcPager使用详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MVC分页之MvcPager使用详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址