关于asp.net mvc的分页,网上已经有很多了。本来也想借用,先看了杨涛写的分页控件,感觉用起来稍微有点复杂,而我只需要简单的分页。分页我写过很多次,原理也熟悉,就是构造首页、上一页、下一页及末页的链接,做得好点,还可以有页码、下拉分页等。于是我又造了一个轮子。
先准备数据,这里以人员信息为例:
public string Name { get ; set ; } |
public int Age { get ; set ; } |
初始化100条数据,并提供一个方法,可以从这些数据中按照分页大小和页码获取。
public class PersonHelper |
private static List<PersonInfo> list; |
list = new List<PersonInfo>(); |
for ( int i = 0; i < 100; i++) |
list.Add( new PersonInfo() |
Name = "姓名" + i.ToString(), |
public static IEnumerable<PersonInfo> GetList( int pageSize, int pageIndex) |
return list.Skip((pageIndex - 1) * pageSize).Take(pageSize); |
Model定义:其中包含了分页大小、当前页码、记录数和人员信息集合。
using System.Collections.Generic; |
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) |
if (pageIndex == null || pageIndex <= 0) |
IEnumerable<PersonInfo> query = PersonHelper.GetList(pageSize, pageIndex.Value); |
NewsModels model = new NewsModels() |
PageIndex = pageIndex.Value, |
View中处理:
@model MvcApplication2.Models.PersonListModels |
ViewBag.Title = "Person List" ; |
@ foreach (MvcApplication2.Code.PersonInfo info in Model.Persons) |
@Url.Pager( "Home" , "PersonList" , Model.PageSize, Model.PageIndex, Model.RecordCount) |
重点就在@Url.Pager的使用了。扩展UrlHelper的代码如下:
public static class HtmlExtend |
/// 扩展UrlHelper,实现输出分页HTML |
/// <param name="urlHelper"></param> |
/// <param name="controllerName">控制器名</param> |
/// <param name="actionName">行为名</param> |
/// <param name="pageSize">分页大小</param> |
/// <param name="pageIndex">当前页码</param> |
/// <param name="recordCount">总记录数</param> |
public static MvcHtmlString Pager( this UrlHelper urlHelper, string controllerName, string actionName, int pageSize, int pageIndex, int recordCount) |
return MvcHtmlString.Create( string .Empty); |
int pageCount = ( int ) decimal .Ceiling(( decimal )recordCount / ( decimal )pageSize); |
string firstStr = string .Empty; |
string lastStr = string .Empty; |
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>" ; |
string preStr = string .Empty; |
if (pageIndex > 1 && pageIndex <= pageCount) |
string prevUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = pageIndex - 1 }); |
preStr = "<a href='" + prevUrl + "'>上一页</a>" ; |
string nextStr = string .Empty; |
if (pageIndex > 0 && pageIndex < pageCount) |
string nextUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = pageIndex + 1 }); |
nextStr = "<a href='" + nextUrl + "'>下一页</a>" ; |
string numStr = string .Empty; |
for ( int i = 1; i <= pageCount; i++) |
string numUrl = urlHelper.Action(actionName, controllerName, new { pageIndex = i }); |
numStr += "[<a href='" + numUrl + "'><strong>" + i + "</strong></a>] " ; |
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/
最新文章
- sharepoint app 开发环境配置
- winform 控件(2)
- Leetcode 88 Merge Sorted Array STL
- 整合spring,springmvc和mybatis
- 程序语言的奥妙:算法解读 &mdash;&mdash;读书笔记
- Objective-C ,ios,iphone开发基础:几个常用类-NSNumber
- a-b(高精度)
- PASCAL的读入优化
- 用Visual Studio 2015 编写驱动之前一定要注意的问题!!!
- 解题思路:best time to buy and sell stock i &;&; ii &;&; iii
- python--批量下载豆瓣图片之升级版本
- Java中不定项参数(可变参数)的作用和使用方式
- QT中子目录调用另一个子目录
- Java中的枚举Enum
- Solr记录-solr基础内容
- Android SDK platforms build-tools等镜像下载
- [LeetCode&;Python] Problem 617. Merge Two Binary Trees
- POJ2392 Space Elevator
- 【PAT】1018 锤子剪刀布 (20)(20 分)
- HDU4548+筛素数
热门文章
- 【BZOJ3831】[Poi2014]Little Bird 单调队列
- [SharePoint 2010] 如何在小組網站內頁面上撥放影片或是音效檔
- 事件处理程序中 this 的指向
- 大文本 mysql es
- git之merge和rebase的区别
- vim的快捷键
- 003-maven简介
- 18.让sublime text3支持Vue语法高亮显示
- (转)理解POST和PUT的区别,顺便提下RESTful
- 玩转DOM遍历——用NodeIterator实现getElementById,getElementsByTagName方法