关于Mvc的分页写法
关于asp.net mvc的分页,网上已经有很多了。本来也想借用,先看了杨涛写的分页控件,感觉用起来稍微有点复杂,而我只需要简单的分页。分页我写过很多次,原理也熟悉,就是构造首页、上一页、下一页及末页的链接,做得好点,还可以有页码、下拉分页等。于是我又造了一个轮子。
先准备数据,这里以人员信息为例:
public class PersonInfo |
{ |
public string Name { get ; set ; } |
public int Age { get ; set ; } |
} |
初始化100条数据,并提供一个方法,可以从这些数据中按照分页大小和页码获取。
public class PersonHelper |
{ |
private static List<PersonInfo> list; |
static PersonHelper() |
{ |
list = new List<PersonInfo>(); |
for ( int i = 0; i < 100; i++) |
{ |
list.Add( new PersonInfo() |
{ |
Name = "姓名" + i.ToString(), |
Age = 18 + i |
}); |
} |
} |
/// <summary> |
/// |
/// </summary> |
/// <returns></returns> |
public static IEnumerable<PersonInfo> GetList( int pageSize, int pageIndex) |
{ |
return list.Skip((pageIndex - 1) * pageSize).Take(pageSize); |
} |
} |
Model定义:其中包含了分页大小、当前页码、记录数和人员信息集合。
using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Web; |
using MvcApplication2.Code; |
|
namespace MvcApplication2.Models |
{ |
public class PersonListModels |
{ |
public int PageIndex { get ; set ; } |
public int PageSize { set ; get ; } |
public int RecordCount { get ; set ; } |
public IEnumerable<PersonInfo> Persons { get ; set ; } |
} |
} |
Controller中的处理:
public ActionResult PersonList( int ? pageIndex) |
{ |
// 分页大小 |
int pageSize = 10; |
|
// 获取分页页码 |
if (pageIndex == null || pageIndex <= 0) |
{ |
pageIndex = 1; |
} |
|
// 获取分页数据 |
IEnumerable<PersonInfo> query = PersonHelper.GetList(pageSize, pageIndex.Value); |
|
// 设置模型 |
NewsModels model = new NewsModels() |
{ |
Persons = query, |
PageIndex = pageIndex.Value, |
PageSize = pageSize, |
RecordCount = 100 |
}; |
|
return View(model); |
} |
View中处理:
@model MvcApplication2.Models.PersonListModels |
@{ |
ViewBag.Title = "Person List" ; |
} |
<h2> |
Person List</h2> |
<table> |
@ foreach (MvcApplication2.Code.PersonInfo info in Model.Persons) |
{ |
<tr> |
<td>@info.Name |
</td> |
<td>@info.Age |
</td> |
</tr> |
} |
<tr> |
<td colspan= "2" > |
@Url.Pager( "Home" , "PersonList" , Model.PageSize, Model.PageIndex, Model.RecordCount) |
</td> |
</tr> |
</table> |
重点就在@Url.Pager的使用了。扩展UrlHelper的代码如下:
namespace System.Web.Mvc |
{ |
public static class HtmlExtend |
{ |
/// <summary> |
/// 扩展UrlHelper,实现输出分页HTML |
/// </summary> |
/// <param name="urlHelper"></param> |
/// <param name="controllerName">控制器名</param> |
/// <param name="actionName">行为名</param> |
/// <param name="pageSize">分页大小</param> |
/// <param name="pageIndex">当前页码</param> |
/// <param name="recordCount">总记录数</param> |
/// <returns></returns> |
public static MvcHtmlString Pager( this UrlHelper urlHelper, string controllerName, string actionName, int pageSize, int pageIndex, int recordCount) |
{ |
// 如果分页大小等于0,则返回空字符串 |
if (pageSize == 0) |
{ |
return MvcHtmlString.Create( string .Empty); |
} |
|
// 根据总记录数和分页大小计算出分页数量 |
int pageCount = ( int ) decimal .Ceiling(( decimal )recordCount / ( decimal )pageSize); |
|
// 首页、末页 |
string firstStr = string .Empty; |
string lastStr = string .Empty; |
if (recordCount > 0) |
{ |
string firstUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = 1 }); |
firstStr = "<a href='" + firstUrl + "'>首页</a>" ; |
|
string lastUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = pageCount }); |
lastStr = "<a href='" + lastUrl + "'>末页</a>" ; |
} |
else |
{ |
firstStr = "首页" ; |
lastStr = "末页" ; |
} |
|
// 上一页 |
string preStr = string .Empty; |
if (pageIndex > 1 && pageIndex <= pageCount) |
{ |
string prevUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = pageIndex - 1 }); |
preStr = "<a href='" + prevUrl + "'>上一页</a>" ; |
} |
else |
{ |
preStr = "上一页" ; |
} |
|
// 下一页 |
string nextStr = string .Empty; |
if (pageIndex > 0 && pageIndex < pageCount) |
{ |
string nextUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = pageIndex + 1 }); |
nextStr = "<a href='" + nextUrl + "'>下一页</a>" ; |
} |
else |
{ |
nextStr = "下一页" ; |
} |
|
// 页码 |
string numStr = string .Empty; |
if (pageCount > 0) |
{ |
// 遍历输出全部的页码 |
for ( int i = 1; i <= pageCount; i++) |
{ |
string numUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = i }); |
|
// 当前页码加粗 |
if (i == pageIndex) |
{ |
numStr += "[<a href='" + numUrl + "'><strong>" + i + "</strong></a>] " ; |
} |
else |
{ |
numStr += "[<a href='" + numUrl + "'>" + i + "</a>] " ; |
} |
} |
} |
|
string pageStr = firstStr + " " + preStr + " " + numStr + nextStr + " " + lastStr; |
|
return MvcHtmlString.Create(pageStr); |
} |
} |
} |
看看效果:
这个扩展没有实现页码分段显示,有兴趣的朋友可以自己试试。
文章来源:http://blog.bossma.cn/asp_net_mvc/asp-net-mvc-url-pager/
标签:
MVC 分页
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2012-11-04 用户权限树的建立及递归算法思路原则