ASP.NET MVC分页实现

ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FORM中,当然以后我会考虑实现基于URL分页的!

一、PageInfo类

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5
  6 namespace ROIS.Models
  7 {
  8     /// <summary>
  9     /// 分页信息
 10     /// </summary>
 11     public class PageInfo
 12     {
 13         private int _RecordCount = 0;
 14         private int _PageSize = 10;
 15         private int _CurrentPageNo=1;
 16
 17         /// <summary>
 18         /// 获取或设置记录总数
 19         /// </summary>
 20         public int RecordCount
 21         {
 22             get
 23             {
 24                 return _RecordCount;
 25             }
 26             set
 27             {
 28                 if (value > 0)
 29                 {
 30                     _RecordCount = value;
 31                 }
 32             }
 33         }
 34
 35         /// <summary>
 36         /// 获取或设置每页记录数
 37         /// </summary>
 38         public int PageSize
 39         {
 40             get {
 41                 return _PageSize;
 42             }
 43             set {
 44                 if (value > 0)
 45                 {
 46                     _PageSize = value;
 47                 }
 48             }
 49         }
 50
 51         /// <summary>
 52         /// 获取或设置当前索引页码(从1开始计算)
 53         /// </summary>
 54         public int CurrentPageNo
 55         {
 56             get {
 57                 return _CurrentPageNo;
 58             }
 59
 60             set {
 61                 if (value > 0)
 62                 {
 63                     if (value > this.PageCount)
 64                     {
 65                         _CurrentPageNo = this.PageCount;
 66                     }
 67                     else
 68                     {
 69                         _CurrentPageNo = value;
 70                     }
 71                 }
 72             }
 73         }
 74
 75         /// <summary>
 76         /// 获取总页数
 77         /// </summary>
 78         public int PageCount
 79         {
 80             get
 81             {
 82                 if (this.RecordCount <= 0)
 83                 {
 84                     return 1;
 85                 }
 86
 87                 return this.RecordCount / this.PageSize + (this.RecordCount % this.PageSize > 0 ? 1 : 0);
 88             }
 89         }
 90
 91         public PageInfo()
 92         { }
 93
 94         public PageInfo(int recordCount, int currentPageNo, int pageSize = 10)
 95         {
 96             this.RecordCount = recordCount;
 97             this.PageSize = pageSize;
 98             this.CurrentPageNo = currentPageNo;
 99         }
100
101
102         /// <summary>
103         /// 是否为首页
104         /// </summary>
105         /// <returns></returns>
106         public bool IsFirstPage()
107         {
108             return (this.CurrentPageNo <= 1);
109         }
110
111
112         /// <summary>
113         /// 是否为末页
114         /// </summary>
115         /// <returns></returns>
116         public bool IsLastPage()
117         {
118             return (this.CurrentPageNo>=this.PageCount);
119         }
120
121     }
122 }
123  

二、_Pager局部视图(建议放在Shared目录下)

@using ROIS.Models;

@model PageInfo

@if (Model!=null && Model.RecordCount > 0)
{
<div class="pager">
    第@(Model.CurrentPageNo) 页&nbsp;/&nbsp;共@(@Model.PageCount)页,
@if (Model.IsFirstPage())
{
    <span>|&lt;首&nbsp;&nbsp;页</span>
    <span>&lt;上一页</span>
}
else
{
    <a href="javascript:turnPage(1);">|&lt;首&nbsp;&nbsp;页</a>
    <a href="javascript:turnPage(@(Model.CurrentPageNo-1));">&lt;上一页</a>
}
@if (Model.IsLastPage())
{
    <span>下一页&gt;</span>
    <span>末&nbsp;&nbsp;页&gt;|</span>
}
else
{
     <a href="javascript:turnPage(@(Model.CurrentPageNo+1));">下一页&gt;</a>
     <a href="javascript:turnPage(@Model.PageCount);">末&nbsp;&nbsp;页&gt;|</a>
}
转到:
<select id="pages" onchange="javascript:turnPage(this.value);">
    @for (int i = 1; i <= Model.PageCount; i++)
    {
        if (Model.CurrentPageNo == i)
        {
        <option value="@i" selected="selected">第@(i)页</option>
        }
        else
        {
        <option value="@i">第@(i)页</option>
        }
    }
</select>
<input type="hidden" id="_pageno" name="_pageno" />
</div>
<script type="text/javascript">
<!--
    function turnPage(pageNo) {
        var oPageNo = document.getElementById("_pageno");
        oPageNo.value = pageNo;
        oPageNo.form.submit();
    }

    function getForm(obj) { //这个没有用到,但可以取代上面的oPageNo.form
        if (obj.parentNode.nodeName.toLowerCase() == "form") {
            return obj.parentNode;
        } else {
            getForm(obj.parentNode);
        }
    }
//-->
</script>

}

三、使用方法:

后台Controller的Action中加入:

string pageNo = Request.Form["_pageno"];
            int iPageNo = 1;
            int.TryParse(pageNo, out iPageNo);
            PageInfo pageInfo=new PageInfo(5000,iPageNo, 20);

ViewBag.PageInfo = pageInfo;

前台VIEW页面代码如下:(注: ROIS是我专案名称,依实际情况更换)

@using (Html.BeginForm())
{
      这里面是数据列表HTML代码

@Html.Partial("_Pager", ViewBag.PageInfo as ROIS.Models.PageInfo)

}

原文出自我的个人网站:http://www.zuowenjun.cn/post/2014/08/26/24.html

时间: 2024-08-03 14:48:13

ASP.NET MVC分页实现的相关文章

ASP.NET MVC 分页MvcPager

ASP.NET MVC 分页MvcPager 开源框架  admin  8个月前 (08-20)  1484浏览 他连续12年获得微软MPV称号,几年前,他写的ASP.NET分页控件,被很多.NET开发人员使用,现在他又写了名为MvcPager的分页扩展,并免费开源,支持MVC通用分页,与EF完美结合,支持AJAX分页,简单灵活,提供多个演示案例,是迄今为止最好的MVC分页方式,推荐各位ASP.NET MVC开发者使用. 分页是每个项目必须面对的技术点,不好的分页不但体验不好,而且会影响系统的整

Asp.Net MVC 分页、检索、排序整体实现

原文:Asp.Net MVC 分页.检索.排序整体实现 很多时候需要这样的功能,对表格进行分页.排序和检索.这个有很多实现的方式,有现成的表格控件.用前端的mvvm,用户控件.但很多时候看着很漂亮的东西你想进一步控制的时候却不那么如意.这里自己实现一次,功能不是高大全,但求一个清楚明白,也欢迎园友拍砖.前端是bootstrap3+jPaginate,后台基于membership.没什么难点. 先上效果图. 分页其实就是处理好 每页项目数.总项目数.总页数.当前页.为了方便复用,就先从仓库开始说起

ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页

我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了更为灵活的配置属性及生成规则,解决了上述问题,代码如下: 一.PageInfo类 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace ROIS.Models {

Asp.net MVC分页实例

分页是网页基本功能,这里主要讨论在Asp.net MVC环境下分页的前端实现,不涉及后台分页.实现效果如下图显示: Step 1.建立分页信息类 1 public class PagingInfo 2 { 3 public int TotalItmes { set; get; } 4 public int ItemsPerPage { set; get; } 5 public int CurrentPage { set; get; } 6 public int TotalPages 7 { 8

【随笔系列】Asp.Net Mvc分页控件PagedList的使用方法及配置

企业在做Asp.Net Mvc开发过程中,很多时候都是一些CRUD,最基本的就是一个列表页面,然后附带一些功能按钮.如果有数据列表,大多数就会涉及到对数据进行分页,这次就介绍一下Mvc PagedList控件分页的使用方法.Github PagedList链接 . 下面我通过新建Mvc项目来展示PagedList的使用方法. 一.新建BookLibrary解决方案 确定后,选择MVC 然后点击确定. 二.添加PagedList与PagedList.Mvc的程序包. 选择BookLibrary项目

基于Bootstrap的Asp.net Mvc 分页的实现

最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这边是模拟了一些假的数据,实际的项目中都是在数据库中去取得的,很简单的数据: 1 public class User 2 { 3 public string Name { get; set; } 4 5 public int Age { get; set; } 6 } 取数据我这边是加了120个数据: 1 publ

ASP.NET MVC分页 Ajax+JsRender

前段时间整mvc的分页,倒是很顺利,参考了以下几篇博客,启发很大. http://www.cnblogs.com/tangmingjun/archive/2012/05/30/2526301.html http://www.cnblogs.com/tangmingjun/archive/2012/05/31/2528732.html 顺便扩展了需求,在分页的基础上,继续做关键字查询. 用PagedList生成的页码链接虽然样式很漂亮,但是要做到无刷新的分页,PagedList自动生成的代码是不够

基于Bootstrap的Asp.net Mvc 分页的实现(转)

最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这 边是模拟了一些假的数据,实际的项目中都是在数据库中去取得的,很简单的数据: public class User { public string Name { get; set; } public int Age { get; set; } } 取数据我这边是加了120个数据: public List<User>

Asp.net MVC 分页,采用 MvcPager 和CYQ.Data来分页

Control: public ActionResult Index(int id=1) { int pageSize = 20; int totalItems = 0; using (MAction action = new MAction("brain")) { MDataTable table = action.Select(id, pageSize, "order by id desc", out totalItems); PagedList<MDat