MVCPager学习小记
1.PageIndexParameterName怎么关联?
答:其实就是Action里面的pageindex参数
例子:

@Html.Pager(Model, new PagerOptions { PageIndexParameterName = "pageindex", ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.DropDownList, ShowGoButton = false })
后台:

public ActionResult Basic(int pageindex = 1) { return View(DemoData.AllArticles.OrderByDescending(a => a.PubDate).ToPagedList(pageindex, 8)); }
2.输入页码无法跳转是怎么回事?
需要添加如下引用:
<script type="text/javascript" src="/Scripts/jquery-1.8.2.min.js"></script>
并通过如下方式注册MvcPager的客户端jQuery插件脚本:
@section Scripts{@{Html.RegisterMvcPagerScriptResource();}}
3.以下各节已定义,但尚未为布局页“~/Views/Shared/_Layout.cshtml”呈现:“Scripts”
嵌套的顶层应该写:
@RenderSection("Scripts", false)
误写成了:
@section Scripts{ @RenderSection("Scripts",false) }
4.有关自定义路由
路由设置成这样:

routes.MapRoute("Paging", "{controller}/{action}/page_{pageindex}", new { controller = "NoDb", action = "CustomRouting", pageindex = 1 }, new { action = "CustomRouting" }); routes.MapRoute("OptionalPaging", "{controller}/{action}/pageindex-{pageindex}", new { controller = "NoDb", action = "CustomRouting", pageindex = 1 }, new { action = "CustomRouting" });
前台调用:

@Html.Pager(Model, new PagerOptions { ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.DropDownList, ShowGoButton = false, FirstPageRouteName = "Default" }, "Paging") @Html.Pager(Model, new PagerOptions { ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.DropDownList, ShowGoButton = false, FirstPageRouteName = "Default" }, "OptionalPaging", null)
注意:如果第二个pager没有设置最后一个参数(routeValues)为null,则两个pager产生的路由信息完全相同,因为MVC中匹配到了第一个路由就把后面那个忽略了。
5.有关Ajax局部加载(不用PartialView的情况)
需要把EnablePartialLoading设为true,如:

@Ajax.Pager(Model, new PagerOptions { PageIndexParameterName = "id", ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.DropDownList, ShowGoButton = false }, new MvcAjaxOptions { UpdateTargetId = "articles", EnablePartialLoading = true })
另外,如果按“下一页”,地址栏有变化,但是数据没有翻页翻过去,需要检查一下是不是BeginForm忘了加:
@Html.BeginForm()
复杂一点的:

@using (Html.BeginForm("AjaxPaging", "NoDb", new { id = "" }, FormMethod.Get, new { id = "searchForm" })) { <span>标题:</span>@Html.TextBoxFor(model => model.title, new { @id = "title", @name = "title", @type = "text", @style = "width:120px" }) <span>作者:</span>@Html.TextBoxFor(model => model.author, new { @id = "author", @name = "author", @type = "text", @style = "width:120px" }) <span>来源:</span>@Html.TextBoxFor(model => model.source, new { @id = "source", @name = "source", @type = "text", @style = "width:120px" }) <input type="submit" value="搜索(S)" accesskey="S" /> }
还有就是,Ajax.BeginForm在这里是行不通的
总的说来,这种方法只适合于简单的情况,还是把分页数据放到PartialView比较好
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步