首页 > 代码库 > ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
<style></style>
using System;
@using ROIS.Models;
我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了更为灵活的配置属性及生成规则,解决了上述问题,代码如下:
一、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;
private string _PageNoCtrlName = "_pageno";
private bool _CreateScript = true;
/// <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);
}
}
/// <summary>
/// 获取或设置保存页码的控件名称
/// </summary>
public string PageNoCtrlName
{
get
{
return _PageNoCtrlName;
}
set
{
if (!string.IsNullOrEmpty(value))
{
_PageNoCtrlName = value;
}
}
}
/// <summary>
/// 是否需要创建脚本函数
/// </summary>
public bool CreateScript
{
get { return _CreateScript; }
set { _CreateScript = value; }
}
public PageInfo()
{ }
public PageInfo(int recordCount, int currentPageNo, int pageSize = 10, string pageNoCtrlName = null, bool createScript = true)
{
this.RecordCount = recordCount;
this.PageSize = pageSize;
this.CurrentPageNo = currentPageNo;
this.PageNoCtrlName = pageNoCtrlName;
this.CreateScript = createScript;
}
/// <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,"@Model.PageNoCtrlName");">|<首 页</a>
<a href="javascript:turnPage(@(Model.CurrentPageNo-1),"@Model.PageNoCtrlName");"><上一页</a>
}
@if (Model.IsLastPage())
{
<span>下一页></span>
<span>末 页>|</span>
}
else
{
<a href="javascript:turnPage(@(Model.CurrentPageNo+1),"@Model.PageNoCtrlName");">下一页></a>
<a href="javascript:turnPage(@Model.PageCount,"@Model.PageNoCtrlName");">末 页>|</a>
}
转到:
<select id="pages" onchange="javascript:turnPage(this.value,"@Model.PageNoCtrlName");">
@for (int i = 1; i <= Model.PageCount; i++)
{
if (Model.CurrentPageNo == i)
{
<option value="http://www.mamicode.com/@i" selected="selected">第@(i)页</option>
}
else
{
<option value="http://www.mamicode.com/@i">第@(i)页</option>
}
}
</select>
<input type="hidden" id="@Model.PageNoCtrlName" name="@Model.PageNoCtrlName" />
</div>
if(Model.CreateScript)
{
<script type="text/javascript">
<!--
function turnPage(pageNo,ctrlId) {
var oPageNo = document.getElementById(ctrlId);
oPageNo.value = http://www.mamicode.com/pageNo;
oPageNo.form.submit();
}
function getForm(obj) {
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(detailList.Count(), iPageNo, 30, "_pageno", false);
ViewBag.PageInfo = pageInfo;
pageNo = Request.Form["_pageno2"];
iPageNo = 1;
int.TryParse(pageNo, out iPageNo);
PageInfo pageInfo2 = new PageInfo(detailList2.Count(), iPageNo, 30, "_pageno2",true);
ViewBag.PageInfo2 = pageInfo2;
ViewBag.PageInfo2 = pageInfo2;
前台VIEW页面代码如下:(注: ROIS是我专案名称,依实际情况更换)
@Html.Partial("_Pager", ViewBag.PageInfo as ROIS.Models.PageInfo)
@Html.Partial("_Pager", ViewBag.PageInfo2 as ROIS.Models.PageInfo)
文章同步发表于我的个人网站:http://www.zuowenjun.cn/post/2014/10/23/65.html
ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。