ASP.NET MVC分页实现
2014-08-26 12:51
344 查看
ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FORM中,当然以后我会考虑实现基于URL分页的!
一、PageInfo类
二、_Pager局部视图(建议放在Shared目录下)
三、使用方法:
后台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
一、PageInfo类
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace ROIS.Models { /// <summary> /// 分页信息 /// </summary> public class PageInfo { private int _RecordCount = 0; private int _PageSize = 10; private int _CurrentPageNo=1; /// <summary> /// 获取或设置记录总数 /// </summary> public int RecordCount { get { return _RecordCount; } set { if (value > 0) { _RecordCount = value; } } } /// <summary> /// 获取或设置每页记录数 /// </summary> public int PageSize { get { return _PageSize; } set { if (value > 0) { _PageSize = value; } } } /// <summary> /// 获取或设置当前索引页码(从1开始计算) /// </summary> public int CurrentPageNo { get { return _CurrentPageNo; } set { if (value > 0) { if (value > this.PageCount) { _CurrentPageNo = this.PageCount; } else { _CurrentPageNo = value; } } } } /// <summary> /// 获取总页数 /// </summary> public int PageCount { get { if (this.RecordCount <= 0) { return 1; } return this.RecordCount / this.PageSize + (this.RecordCount % this.PageSize > 0 ? 1 : 0); } } public PageInfo() { } public PageInfo(int recordCount, int currentPageNo, int pageSize = 10) { this.RecordCount = recordCount; this.PageSize = pageSize; this.CurrentPageNo = currentPageNo; } /// <summary> /// 是否为首页 /// </summary> /// <returns></returns> public bool IsFirstPage() { return (this.CurrentPageNo <= 1); } /// <summary> /// 是否为末页 /// </summary> /// <returns></returns> public bool IsLastPage() { return (this.CurrentPageNo>=this.PageCount); } } }
二、_Pager局部视图(建议放在Shared目录下)
@using ROIS.Models; @model PageInfo @if (Model!=null && Model.RecordCount > 0) { <div class="pager"> 第@(Model.CurrentPageNo) 页 / 共@(@Model.PageCount)页, @if (Model.IsFirstPage()) { <span>|<首 页</span> <span><上一页</span> } else { <a href="javascript:turnPage(1);">|<首 页</a> <a href="javascript:turnPage(@(Model.CurrentPageNo-1));"><上一页</a> } @if (Model.IsLastPage()) { <span>下一页></span> <span>末 页>|</span> } else { <a href="javascript:turnPage(@(Model.CurrentPageNo+1));">下一页></a> <a href="javascript:turnPage(@Model.PageCount);">末 页>|</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
相关文章推荐
- 【EntityFramework系列教程三,翻译】在ASP.NET MVC程序中使用EntityFramework对数据进行排序、过滤筛选以及实现分页
- dhl:asp.net mvc下用jQuery ajax方法实现无刷新分页(用户控件)& 整体页面无刷新(2010-11-13 add)
- 在ASP.NET MVC中使用Boostrap实现产品的展示、查询、排序、分页
- asp.net mvc分页的实现 mvcpaging
- asp.net mvc基于jQuery+Ajax实现无刷新分页
- Asp.Net MVC 分页、检索、排序整体实现
- Asp.Net MVC 分页、检索、排序整体实现代码
- asp.net mvc 简易通用自定义Pager实现分页
- ASP.NET MVC分页实现
- ASP.NET MVC 简单的分页思想与实现
- ASP.NET MVC分页的实现(下)
- ASP.NET MVC分页的实现(上)
- 艾伟_转载:ASP.NET MVC分页的实现
- ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
- ASP.NET MVC分页和排序功能实现
- asp.net mvc 分页的完整实现及源码下载
- ASP.NET MVC 3.0(十九): MVC 3.0 实例之使用开源控件实现表格排序和分页
- ASP.NET MVC 简单的分页思想与实现
- ASP.NET MVC分页的实现
- ASP.NET MVC分页的实现