首页 > 代码库 > PagedList.MVC分页

PagedList.MVC分页

?
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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中