ASP.NET MVC- 使用PageList.Mvc分页
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ASP.NET MVC- 使用PageList.Mvc分页,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3669字,纯文字阅读大概需要6分钟。
内容图文
ASP.NET MVC中进行分页的方式有多种,在NuGet上有提供使用PagedList、PagedList.Mvc进行分页。
1. 通过NuGet引用PagedList.Mvc
在安装引用PagedList.Mvc的同时会安装引用PagedList。
1.看一下Controller页面的代码,最后就简单的一句,将List数据ToPagedList返回过去就可以了。原来爱怎么分页可以怎么分页。
1 // 引用 2 using PagedList; 3 namespace MvcApplication1.Controllers 4 { 5 public class SplitPageController : Controller 6 { 7 // 8 // GET: /SplitPage/ 9 10 public ActionResult Index(int page = 1) 11 { 12//取数据13 List<Package.Model.Message> lists = new List<Package.Model.Message>(); 14 MessageDal message = new MessageDal(); 15 DataSet ds = message.GetList(" 1=1 "); 1617//转成List18for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 19 { 20 lists.Add(new Package.Model.Message() 21 { 22 gName = ds.Tables[0].Rows[i]["gName"].ToString(), 23 gContent = ds.Tables[0].Rows[i]["gContent"].ToString() 24 }); 25 } 2627//将数据分页后返回页面(这里可以在取数据的时候先分页后,再使用ToPagedList实现分页28return View( lists.ToPagedList(page, 2)); 29 } 3031 } 32 }
返回后在HTML页面的显示
1 @model PagedList.IPagedList<Package.Model.Message> 2@using PagedList.Mvc; 3 4@{ 5 Layout = null; 6} 7 8<!DOCTYPE html> 910<html>11<head>12<meta name="viewport" content="width=device-width"/>13<title>Index</title>14</head>15<body>16<table>17<tr>18<th>19 gname 20</th>21<th>22 gContent 23</th>24<th>2526</th>27</tr>28 @foreach (var item in Model) 29 { 30<tr>31<td>32 @Html.DisplayFor(modelItem => item.gName) 33</td>34<td>35 @Html.DisplayFor(modelItem => item.gContent) 36</td>37<td></td>38</tr>39 } 40</table>41 @Html.PagedListPager(Model, page => Url.Action("Index", new { page })) 42</body>43</html>
使用ViewBag返回分页数据,由于MODEL只有一个,如果按官网给的例子,那么一个页面只能有一个分页了,但是VIEWBAG点出来的却可以多个。
看一下显示,不多说了,直接上代码
1 using PagedList; 2 3 namespace MvcApplication2.Controllers 4 { 5 public class HomeController : Controller 6 { 7 // 8 // GET: /Home/ 9 10 public ActionResult Index(int page = 1) 11 { 12//取数据13 List<Package.Model.Message> lists = new List<Package.Model.Message>(); 14 MessageDal message = new MessageDal(); 15 DataSet ds = message.GetList(" 1=1 "); 1617//转成List18for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 19 { 20 lists.Add(new Package.Model.Message() 21 { 22 gName = ds.Tables[0].Rows[i]["gName"].ToString(), 23 gContent = ds.Tables[0].Rows[i]["gContent"].ToString() 24 }); 25 } 2627//将数据分页后返回页面(这里可以在取数据的时候先分页后,再使用ToPagedList实现分页28 ViewBag.MyPageList = lists.ToPagedList(page, 2); 29return View( ); 30 } 3132 } 33 }
VIEW视图
1 @using PagedList.Mvc; 2 3 @{ 4 PagedList.IPagedList<Package.Model.Message> myPageList = (PagedList.IPagedList<Package.Model.Message>)ViewBag.MyPageList; 5 Layout = null; 6} 7 8<!DOCTYPE html> 910<html>11<head>12<meta name="viewport" content="width=device-width"/>13<link href="~/Content/PagedList.css" rel="stylesheet"/>14<title>Index</title>15</head>16<body>17<table>18<tr>19<th>20 gname 21</th>22<th>23 gContent 24</th>25<th>2627</th>28</tr>29 @foreach (var item in myPageList) 30 { 31<tr>32<td>33 @Html.DisplayFor(modelItem => item.gName) 34</td>35<td>36 @Html.DisplayFor(modelItem => item.gContent) 37</td>38<td></td>39</tr>40 } 41</table>42 每页 @myPageList.PageSize 条记录,共 @myPageList.PageCount 页,当前第 @myPageList.PageNumber 页 43 @Html.PagedListPager(myPageList, page => Url.Action("Index", new { page })) 44</body>45</html>
原文:https://www.cnblogs.com/softwyy/p/8685085.html
内容总结
以上是互联网集市为您收集整理的ASP.NET MVC- 使用PageList.Mvc分页全部内容,希望文章能够帮你解决ASP.NET MVC- 使用PageList.Mvc分页所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。