mvc使用x.pagedlist分页

  1. Nuget安装 X.PagedList和X.PagedList.MVC
    package.config文件:

    1
    2
    <package id="X.PagedList" version="1.24.1.300" targetFramework="net45" />
    <package id="X.PagedList.Mvc" version="4.9.1.310" targetFramework="net45" />
  2. View界面:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    @model  PagedList.IPagedList<T>
    @using Model
    @using PagedList
    @using PagedList.Mvc


    <div style="text-align: center;">
    <h1>Mvc分页例子</h1>
    <table id="tbList">
    <tbody>
    @if (Model.Count() != 0)
    {
    <tr>
    <th style="width: 60px;">编号</th>
    <th style="width: 150px;">创建时间</th>
    <th style="width: 200px;">标题</th>
    <th style="width: 250px;">内容</th>
    <th style="width: 80px;">查看次数</th>
    <th style="width: 180px;">图片</th>
    </tr>
    foreach (var a in Model)
    {
    <tr style="text-align: center;">
    <td>@a.id</td>
    <td>@a.createtime</td>
    <td>@a.title</td>
    <td>@a.body</td>
    <td>@a.visitnum</td>
    <td>@a.img</td>
    </tr>
    }
    <tr>
    <td colspan="6">
    <div class="pager">
    @if (Model != null)
    {
    <span style="height: 20px; line-height: 20px;">
    共 @Model.TotalItemCount.ToString() 条记录,
    当前第 @ViewData["page"] 页/共 @Model.PageCount 页
    </span>
    @Html.PagedListPager(Model, page => Url.Action("TableByChaJ", new { page }), new PagedListRenderOptions() { LinkToFirstPageFormat = "首页", LinkToNextPageFormat = "下一页", LinkToPreviousPageFormat = "上一页", LinkToLastPageFormat = "末页", DisplayItemSliceAndTotal = false, MaximumPageNumbersToDisplay = 3 })
    }
    </div>
    </td>
    </tr>
    }
    </tbody>
    </table>
    </div>

注意:PagedList.IPagedList 中T 改成自己分页的实体类

  1. controller控制器:
    1
    2
    3
    4
    5
    6
    7
    8

    public ActionResult ActionName(int? page)
    {
    int PageNumber = page ?? 1;//page为null时默认值为1
    int PageSize = 5;
    ViewData["page"] = PageNumber;
    return View(VideoDAL.GetListPage().ToPagedList(PageNumber, PageSize));
    }

注意:代码中的list为list 形式,返回视图IPagedList