如何使用 PagedList.Mvc 分页

     刚开始找PagedList分页不是例子太复杂,就是写的过于简略,由于对于MVC的分页不太了解,之前使用的都是Asp.Net 第三方控件 + 数据库存储过程分页。还是老外写的例子简捷,https://github.com/TroyGoode/PagedList  。

     1、本实例采用MVC + EF 框架,至于怎么配置,就不再这里说了。

    

     2、安装 "PagedList.Mvc"的同时 “PagedList”会自动安装

    路径:解决方案资源管理器 -> 选中工程名,点击右键 -> 管理NuGet程序包

   

     首先选中联机,再选中 “PagedList.Mvc” 进行安装

    

     3、在控制器HomeController.cs 中添加代码

         3.1 添加命名空间

    

using PagedList.Mvc;
using PagedList;
View Code

        3.2 在Index方法中添加代码

 1         #region 0.7 查询文章列表--分页 + ActionResult Index()
 2         /// <summary>
 3         /// 查询 文章 列表 并分页
 4         /// </summary>
 5         /// <returns></returns>
 6         public ActionResult Index(int?page)
 7         {
 8             var query = db.BlogArticles.Where(d => d.AIsDel == false).OrderByDescending(d => d.AId) ;
 9             var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
10             var onePageOfProducts = query.ToPagedList(pageNumber, 5); // will only contain 25 products max because of the pageSize
11             ViewBag.OnePageOfProducts = onePageOfProducts;
12 
13             return View();
14         }
15         #endregion
View Code

    4、在视图Index.cshtml中添加代码
        4.1 在添加代码前,现在编译一下代码,否在在视图中添加命名空间会提示”缺少引用命名空间“的语法错误,解决方案资源管理器->选中工程点击右键->重新生成

        4.2  添加命名空间

@using PagedList;
@using PagedList.Mvc;
View Code

      

        4.3  显示文章列表

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <title>Index</title>
 7     <link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />
 8     <style type="text/css">
 9        #tbList
10        {
11            border:1px solid #0094ff;
12            border-collapse:collapse;
13            margin:10px auto;
14            800px;
15        }
16        #tbList th,td
17        {
18            border:1px solid #0094ff;
19            padding:10px;
20            }
21     </style>
22     <script type="text/javascript">
23        function del(Aid) {
24             if (confirm("确定要删除吗?"))
25                 window.location = "/Home/Del/" + Aid; 
26         }
27     </script>
28 </head>
29 <body>
30     <table id="tbList">
31        <tr>              
32           <th>id</th>
33           <th>标题</th>
34           <th>分类</th>
35           <th>状态</th>
36           <th>时间</th>
37           <th>操作</th>
38        </tr>
39     @foreach (var a in ViewBag.OnePageOfProducts) 
40     {
41         <tr>              
42           <td>@a.AId</td>
43           <td>@a.ATitle</td>
44           <td>@a.BlogArticleCate.Name</td>
45           <td>@a.Enumeration.e_cname</td>
46           <td>@a.AAddtime</td>
47           <td>
48              <a href="javascript:del(@a.AId)">删</a>
49              <a href="/Home/Modify/@a.AId">改</a>
50           </td>
51        </tr>
52     }   
53    </table>
View Code

    
       4.4  分页

   <div  style = "margin:10px auto; text-align:center;">
       @Html.PagedListPager((IPagedList)ViewBag.OnePageOfProducts, page => Url.Action("Index", new { page }))
   </div>
</body>
</html>
View Code
原文地址:https://www.cnblogs.com/luyuwei/p/3354958.html