关于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的分页写法的更多相关文章

  1. 基于存储过程的MVC开源分页控件--LYB.NET.SPPager

    摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件MVCPager(http://www.webdiyer.com/)算 ...

  2. 基于存储过程的MVC开源分页控件

    基于存储过程的MVC开源分页控件--LYB.NET.SPPager 摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件M ...

  3. Mvc自定义分页控件

    MVC开发分页常常使用第三方控件,生成的分页HTML带有版权申明,虽然免费,但是总有的别扭.于是,某日,楼主闲来蛋疼,折腾了个自定义分页控件: 先来展示下效果图: 1>当分页不超过10页的时候, ...

  4. MVC简单分页

    对Car汽车表分页 实现简单分页,放在这里方便查看回顾,自定义每页几条有点问题,有待完善······ 1.新建mvc项目 2.添加linq to sql 数据库连接 3.添加CarBF类 using ...

  5. MVC快速分页

    .NET手记-ASP.NET MVC快速分页的实现   对于Web应用,展示List是很常见的需求,随之而来的常见的分页组件.jQuery有现成的分页组件,网上也有着大量的第三方分页组件,都能够快速实 ...

  6. MVC自定义分页

    MVC自定义分页 之前我发表了一篇MVC无刷新分页的文章,里面用的是MvcPager控件,但是那个受那个控件限制,传值只能用PagedList,各方面都受到了限制,自由度不够高,现在还是做MVC无刷新 ...

  7. 学习ASP.NET MVC(十一)——分页

    在这一篇文章中,我们将学习如何在MVC页面中实现分页的方法.分页功能是一个非常实用,常用的功能,当数据量过多的时候,必然要使用分页.在今天这篇文章中,我们学习如果在MVC页面中使用PagedList. ...

  8. ASP.NET MVC 简单分页代码

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  9. [Oracle]关于Oracle分页写法的性能分析及ROWNUM说明

    关于分页写法的性能分析及ROWNUM的补充说明 分页写法 一.测试前数据准备 SQL> SELECT COUNT(*) FROM BPM_PROCVAR; COUNT(*) ---------- ...

随机推荐

  1. HDU 5900 QSC and Master

    题目链接:传送门 题目大意:长度为n的key数组与value数组,若相邻的key互斥,则可以删去这两个数同时获得对应的两 个value值,问最多能获得多少 题目思路:区间DP 闲谈: 这个题一开始没有 ...

  2. LA5059 Playing With Stones

    题意:nim游戏.加上限制每次不得取走超过当前堆一半的石子 1 ≤ N ≤ 100,1 ≤ ai ≤ 2 ∗ 1018 分析:由于ai过大.所以我们采用SG函数递推找规律. (详见代码) #inclu ...

  3. Python 打包程序

    一.打包成exe 1.安装pyinstaller #只要你能FQ连接https://pypi.python.org/pypi下载会很快,不用担心超时问题. https://pypi.python.or ...

  4. ios UITableView高度自适应(转)

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // ...

  5. Sonnet-十四行诗

    <Wish> Of our best wishes we could desire increase, That thereby rose's aroma might never die, ...

  6. ehcache 的 配置文件: ehcache.xml的认识

    <ehcache> <!-- 指定一个目录:当 EHCache 把数据写到硬盘上时, 将把数据写到这个目录下. --> <diskStore path="d:\ ...

  7. Hibernate中双向的一对多关系

    何为双向,双向的意思就是你我之间可以互相通信(customer(1)和order(n)) 也就是说customer可以访问order,order也可以访问customer 二者构成了双向的关系 在Hi ...

  8. kibana5.6源码分析3--目录结构

    kibana5.6的项目目录结构: bin:系统启动脚本目录 config:kibana配置文件目录 data:估计是缓存一些系统数据的,uuid放在这里面 docs: maps:此目录包含TileM ...

  9. wf-删除所选

    w框架-结合用户的不同点击路径,提交正确的id集合. <table class="table"> <tr> <td></td> &l ...

  10. Java8 新特性之Stream API

    1. Stream 概述 Stream 是Java8中处理集合的关键抽象概念,可以对集合执行非常复杂的查找,过滤和映射数据等操作; 使用 Stream API 对集合数据进行操作,就类似于使用 SQL ...