详解MVC分页之MvcPager的使用操作
2019-08-09 05:50:02来源:爱站网 阅读 ()
不管用什么来开发程序,都需要对页面进行分页,目前,越来越多的人使用ASP.NET MVC开发Web应用程序,这将会出现相应的辅助和辅助方法和控制,以提高开发效率,今天爱站技术频道将详解MVC分页之MvcPager的使用操作。
一.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 name="id">pageIndex</param> /// <param name="key">关键字</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 name="viewport" content="width=device-width" /> <title>MVCPager-AjaxPaging</title> <link href="~/Content/pagerstyles.css" rel="stylesheet" /> <link href="~/Content/bootstrap.css" rel="stylesheet" /> </head> <body> <div style="padding: 15px;"> @using (Html.BeginForm("AjaxPaging", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get)) { @Html.Label("关键字:") <input name="key" value="@Request.QueryString["key"]" /><input type="submit" value="查询" /> } @*分页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\"><a href=\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=\"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 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<Article> Articles { get; set; } }
Control:?
/// <summary> /// 同步分页测试 /// </summary> /// <param name="id">pageIndex</param> /// <param name="key">关键字</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 name="viewport" content="width=device-width" /> <title>MVCPager</title> <link href="~/Content/pagerstyles.css" rel="stylesheet" /> <link href="~/Content/bootstrap.css" rel="stylesheet" /> </head> <body> <div style="padding:15px;"> @using (Html.BeginForm("MVCPager", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get)) { @Html.Label("关键字:")<input name="key" value="@Request.QueryString["key"]" /><input type="submit" value="查询" /> } <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\"><a href=\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>", Id = "bootstrappager" }) </nav> </div> </div> </body> </html>
获取测试数据方法(共用):
public class MyTest { /// <summary> /// 获取测试数据 /// </summary> /// <param name="key"></param> /// <param name="PageSize"></param> /// <param name="CurrentCount"></param> /// <param name="TotalCount"></param> /// <returns></returns> public List<Article> 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<Article> list = new List<Article>(); 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的使用操作,希望今天这篇文章能给大家带来帮助,喜欢的网友们赶紧按照爱站技术频道小编的操作试试吧!
原文链接:https://js.aizhan.com/develop/aspnet/7914.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Asp.net MVC SignalR来做实时Web聊天实例代码 2020-03-29
- ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据 2020-03-29
- ASP.NET MVC Admin主页快速构建 2020-03-23
- ASP.NET使用AjaxPro实现前端跟后台交互详解 2020-03-19
- Asp.Net MVC4通过id更新表单内容的思路详解 2020-03-19
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash