MVCPager学习小记

1.PageIndexParameterName怎么关联?

答:其实就是Action里面的pageindex参数

例子:

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

后台:

        public ActionResult Basic(int pageindex = 1)
        {
            return View(DemoData.AllArticles.OrderByDescending(a => a.PubDate).ToPagedList(pageindex, 8));
        }
View Code

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" });
View Code

前台调用:

@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)
View Code

注意:如果第二个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 
    })
View Code

另外,如果按“下一页”,地址栏有变化,但是数据没有翻页翻过去,需要检查一下是不是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 = "120px" }) 
    <span>作者:</span>@Html.TextBoxFor(model => model.author, new { @id = "author", @name = "author", @type = "text", @style = "120px" }) 
    <span>来源:</span>@Html.TextBoxFor(model => model.source, new { @id = "source", @name = "source", @type = "text", @style = "120px" }) 
    <input type="submit" value="搜索(S)" accesskey="S" />
}
View Code

还有就是,Ajax.BeginForm在这里是行不通的

总的说来,这种方法只适合于简单的情况,还是把分页数据放到PartialView比较好

原文地址:https://www.cnblogs.com/zhengwk/p/5319250.html