时间:2021-05-26
最近刚刚接触MVC不久,因项目中要用到分页,网上找了下资料,最后采用了MvcPager(http:///),支持同步和Ajax异步分页。废话不多说了直接上代码。
一.MvcPager异步
ViewModel:
Control:
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(此处可不增加,直接和异步的共用同一个):
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>获取测试数据方法(共用):
效果图:(需要引用CSS)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
初学MVC,做了个单页面应用,需要显示多个分页,并无刷新更新。找到了MvcPager控件,非常好用,在使用ajax过程中遇到很多问题。慢慢调试和杨老师(MvcP
使用方法:先把mvcpager.dll引用加入mvc项目中。前台代码前台:@{Layout=null;}@usingWebdiyer.WebControls.M
ASP.NETMVC中进行分页的方式有多种,在NuGet上有提供使用PagedList、PagedList.Mvc进行分页。在安装引用PagedList.Mvc
1、下载MvcPager.dll文件并引用到MVC项目中2、在控制器中引用命名空间usingWebdiyer.WebControls.Mvc;3、获取数据集合,
最近接触了一下MvcPager,来做个笔记吧其实,我喜欢前后端分离,分页这种东西前端负责的地方,后端不用顾问,这里的MvcPager有点让我想起服务器控件,毕竟