使用資料分頁 using PagedList.Mvc

 

我就列出基本使用步驟

1.先開一個MVC專案

2.於Package Manager Console安裝PagedList

PM> Install-Package PagedList

PM> Install-Package PagedList.Mvc

3.先準備好Model我以北風資料庫的products為例

4.修改Controller為

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using learnningPagedList.Models;
using PagedList;

namespace learnningPagedList.Controllers
{
    public class HomeController : Controller
    {
        NorthwindEntities db = new NorthwindEntities();

        private int pageSize = 5;

        public ActionResult Index(int? page)
        {
            var products = db.Products.OrderBy(x => x.ProductID);

            var pageNumeber = page ?? 1;

            var onePageOfProducts = products.ToPagedList(pageNumeber, pageSize);

            return View(onePageOfProducts);
        }
    }
}

 

注意事項為:

(1).重要的參數有pageSize代表每一個page會帶有幾項資料

(2).使用ToPagedList時必須先OrderBy

(3).注意要把PagedList與PagedList.Mvc Include進來

 

5.由Controller重新產生View,並將View改造成

@model IPagedList<learnningPagedList.Models.Product>

@using PagedList;
@using PagedList.Mvc;

@{
    ViewBag.Title = "Index";
}

@section styles
{
    <link href="~/Content/PagedList.css" rel="stylesheet" />
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().ProductName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().SupplierID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().CategoryID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().QuantityPerUnit)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().UnitPrice)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().UnitsInStock)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().UnitsOnOrder)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().ReorderLevel)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().Discontinued)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ProductName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SupplierID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CategoryID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.QuantityPerUnit)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UnitPrice)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UnitsInStock)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UnitsOnOrder)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ReorderLevel)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Discontinued)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.ProductID }) |
                @Html.ActionLink("Details", "Details", new { id = item.ProductID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.ProductID })
            </td>
        </tr>
    }

</table>

@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))

範例檔

其實Kevin大已經講得夠詳細了,

而我這邊只是作個實例罷了,請參考。

 

參考資料:

ASP.NET MVC 資料分頁操作 - 使用 PagedList.Mvc @ GitHub