PagedList.MVC分页

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PagedList.MVCWeb.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index(int pageIndex=1)
        {
            int vPageCount = 0;
            List<Product> vProductList = GetPageSource(pageIndex, out vPageCount);
            StaticPagedList<Product> vStaticPagedList = new StaticPagedList<Product>(vProductList, pageIndex, 3, vPageCount);
            return View(vStaticPagedList);
        }

        // 可改成读数据库
        [NonAction]
        List<Product> GetPageSource(int argPageindex,out int argPageIndex)
        {
            int vPageSize=3;
            List<Product> vProductList = DataSource();
            argPageIndex = vProductList.Count();
            return vProductList.Skip((argPageindex - 1) * vPageSize).Take(vPageSize).ToList();
        }

        [NonAction]
        List<Product> DataSource()
        {
            return new List<Product>() { 
                    new Product() { 
                        ID=1,
                        Name="Name1",
                        Url="URL1",
                        Price=1,
                        CreateDate=DateTime.Now
                    } ,
                    new Product() { 
                        ID=2,
                        Name="Name2",
                        Url="URL2",
                        Price=2,
                        CreateDate=DateTime.Now
                    } ,
                    new Product() { 
                        ID=3,
                        Name="Name3",
                        Url="URL3",
                        Price=3,
                        CreateDate=DateTime.Now
                    } ,
                    new Product() { 
                        ID=4,
                        Name="Name4",
                        Url="URL4",
                        Price=4,
                        CreateDate=DateTime.Now
                    } ,
                    new Product() { 
                        ID=5,
                        Name="Name5",
                        Url="URL5",
                        Price=5,
                        CreateDate=DateTime.Now
                    } ,
                    new Product() { 
                        ID=6,
                        Name="Name6",
                        Url="URL6",
                        Price=6,
                        CreateDate=DateTime.Now
                    } ,
                    new Product() { 
                        ID=7,
                        Name="Name7",
                        Url="URL7",
                        Price=7,
                        CreateDate=DateTime.Now
                    } ,
                    new Product() { 
                        ID=8,
                        Name="Name8",
                        Url="URL8",
                        Price=8,
                        CreateDate=DateTime.Now
                    } 
            };
        }
    }
}

Index View

@using PagedList.Mvc
@using PagedList.MVCWeb
@model PagedList.StaticPagedList<Product>
 
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="~/Content/PagedList.css" rel="stylesheet" />
    <title>Index</title>
</head>
<body>
    @using(Html.BeginForm())
    {
        <div>
            <table>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>URL</th>
                    <th>Price</th>
                    <th>CreteDate</th>
                </tr>
                   @foreach (Product item in Model)
                   {
                        <tr>
                               <td>@item.ID</td>
                               <td>@item.Name</td>
                               <td>@item.Url</td>
                               <td>@item.Price</td>
                               <td>@item.CreateDate</td>
                        </tr>
                   }
                </table>

            @Html.PagedListPager((PagedList.IPagedList)Model, x => Url.Action("Index", new {pageIndex=x}))
        </div>
    }
</body>
</html>

装了PagedList.Mvc,”~/Content/PagedList.css“NuGet会自动的放在你的Content中

原文地址:https://www.cnblogs.com/xuxu-dragon/p/3777432.html