关于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/
- 基于存储过程的MVC开源分页控件--LYB.NET.SPPager
摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件MVCPager(http://www.webdiyer.com/)算 ...
- 基于存储过程的MVC开源分页控件
基于存储过程的MVC开源分页控件--LYB.NET.SPPager 摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件M ...
- Mvc自定义分页控件
MVC开发分页常常使用第三方控件,生成的分页HTML带有版权申明,虽然免费,但是总有的别扭.于是,某日,楼主闲来蛋疼,折腾了个自定义分页控件: 先来展示下效果图: 1>当分页不超过10页的时候, ...
- MVC简单分页
对Car汽车表分页 实现简单分页,放在这里方便查看回顾,自定义每页几条有点问题,有待完善······ 1.新建mvc项目 2.添加linq to sql 数据库连接 3.添加CarBF类 using ...
- MVC快速分页
.NET手记-ASP.NET MVC快速分页的实现 对于Web应用,展示List是很常见的需求,随之而来的常见的分页组件.jQuery有现成的分页组件,网上也有着大量的第三方分页组件,都能够快速实 ...
- MVC自定义分页
MVC自定义分页 之前我发表了一篇MVC无刷新分页的文章,里面用的是MvcPager控件,但是那个受那个控件限制,传值只能用PagedList,各方面都受到了限制,自由度不够高,现在还是做MVC无刷新 ...
- 学习ASP.NET MVC(十一)——分页
在这一篇文章中,我们将学习如何在MVC页面中实现分页的方法.分页功能是一个非常实用,常用的功能,当数据量过多的时候,必然要使用分页.在今天这篇文章中,我们学习如果在MVC页面中使用PagedList. ...
- ASP.NET MVC 简单分页代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- [Oracle]关于Oracle分页写法的性能分析及ROWNUM说明
关于分页写法的性能分析及ROWNUM的补充说明 分页写法 一.测试前数据准备 SQL> SELECT COUNT(*) FROM BPM_PROCVAR; COUNT(*) ---------- ...
随机推荐
- erase操作
#include<iostream> #include <vector> int main() { std::vector<int> vec; vec.push_b ...
- [C++] 跨平台的生成GUID方法
string GetGUID() { char szGUID[BUFF_SIZE]; #ifdef WIN32 GUID uuid; CoCreateGuid(&uuid); #else Tm ...
- vim学习选取多行(转)
在可视化模式下,可以对一个文本块的整体进行操作.例如,首先高亮选中一部分文本,然后用d命令删除这个文本块.可视化模式的好处在于,你可以在做改动之前,就看到操作将影响的文本.可视化模式可以分为以下三种: ...
- ios开发 更改状态栏
设置statusBar 简单来说,就是设置显示电池电量.时间.网络部分标示的颜色, 这里只能设置两种颜色: 默认的黑色(UIStatusBarStyleDefault) 白色(UIStatusBarS ...
- __destruct()析构函数的执行时刻 __construct()构造函数传入参数 构造函数与后台登录安全
<?php class test_construct_avg { function __construct($input = '') { $this->input = $input; } ...
- VS调用python方法
1. 安装python3.7 2. Vs2010中配置python: 3.添加头文件:#include <Python.h> 4.问题:error LNK2001: 无法解析的外部符号 ...
- WSGI的理解 perfect
https://blog.csdn.net/hzrandd/article/details/10099871 https://blog.csdn.net/cloudxli/article/detail ...
- 为什么在Java中不使用finalize()方法
我们都知道finalize()方法是回收分配给对象的内存之前调用垃圾收集器线程的基本语句.在这篇文章中,我们将会深入这个方法. 这篇文章中的章节: 1.finalize()方法不能保证执行(这个将要用 ...
- mysql 创建函数ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_f
mysql 创建函数的时候 报错 ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL D ...
- TuShare获取K线数据
Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采集.清洗加工 到 数据存储的过程,能够为金融分析人员提供快速.整洁.和多样的便于分析的数据,为他们在数据获取 ...