Fork me on GitHub

MvcPager 分页示例 — Ajax动态加载

该示例演示如何在需要时通过Ajax方法动态加载MvcPager和分页数据。

通过Ajax动态加载MvcPager时,需要在包含MvcPager的PartialView中用Ajax.LoadMvcPagerScript();方法动态加载MvcPager客户端脚本库,而不是在主页面用Html.RegisterMvcPagerScriptResource();方法来注册。

View:

@model PagedList<article>
<button id="loadbtn">点此加载MvcPager和分页数据</button>
<div id="articles">
</div>
@section scripts
{
        <script type="text/javascript">
    $(function () {
        $("#loadbtn").click(function () {
            $("#articles").load(location.href);
            $(this).hide();
        });
    });
    </script>
}

_LoadByAjax.cshtml

@model PagedList<article>
@{ Html.RenderPartial("_ArticleTable"); }
    @Ajax.Pager(Model, new PagerOptions { PageIndexParameterName = "id", ContainerTagName = "ul", PrevPageText = "上页", NextPageText = "下页", FirstPageText = "首页", LastPageText = "尾页", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=\"active\"><a href=\"#\">{0}</a></li>",DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>",PagerItemTemplate = "<li>{0}</li>" }, new MvcAjaxOptions { UpdateTargetId = "articles" })
<script type="text/javascript">
    $(function () {
        @{ Ajax.LoadMvcPagerScript(); }
    });
</script>

_ArticleTable.cshtml:

@model PagedList<Article>
<table class="table table-bordered table-striped">
    <tr>
        <th class="nowrap">序号</th>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PubDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Author)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Source)
        </th>
    </tr>
    @{ int i = 0;}
    @foreach (var item in Model)
    {
        <tr>
            <td>@(Model.StartItemIndex + i++)</td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PubDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Author)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Source)
            </td>
        </tr>
    }
</table>

Model:

    public class Article
    {
        [Display(Name="文章编号")]
        public int ID { get; set; }
        [Display(Name="文章标题")]
        [MaxLength(200)]
        public string Title { get; set; }
        [Display(Name = "文章内容")]
        public string Content { get; set; }
        [Display(Name = "发布日期")]
        public DateTime PubDate { get; set; }
        [Display(Name = "作者")]
        [MaxLength(20)]
        public string Author { get; set; }
        [Display(Name = "文章来源")]
        [MaxLength(20)]
        public string Source { get; set; }
    }

Controller:

        public ActionResult LoadByAjax(int id = 1)
        {
            using (var db = new DataContext())
            {
                var model = db.Articles.OrderByDescending(a => a.PubDate).ToPagedList(id, 5);
                if (Request.IsAjaxRequest())
                {
                    return PartialView("_LoadByAjax", model);
                }
                return View();
            }
        }