MVC做分页功能

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

namespace JPJC.Controllers
{
///
/// 分页类
///
/// 集合
public class Page : List
{
///
/// 索引页
///
public int PageIndex { get; set; }
///
/// 每页显示几条记录
///
public int PageSize { get; set; }
///
/// 一共几页
///
public int PageCount { get; set; }
///
/// 一共多少条记录
///
public int AllCount { get; set; }

///
/// 判断上一页
///
public bool HasPrevious
{
get
{
return PageIndex < PageCount - 1;
}
}
///
/// 判断下一页
///
public bool HasNext
{
get
{
return PageIndex > 0;
}
}
///
/// 翻页函数
///
///
IQueryable 数据
///
索引页
///
每页多少条
public Page(IQueryable source, int pageindex, int pagesize)
{
this.AllCount = source.Count();
this.PageIndex = pageindex;
this.PageSize = pagesize;
//总记录数与每页最大记录数求余
if (this.AllCount % this.PageSize == 0)
this.PageCount = Convert.ToInt32(this.AllCount / this.PageSize);
else
this.PageCount = Convert.ToInt32(this.AllCount / this.PageSize + 1);

if (this.PageIndex > this.PageCount) this.PageIndex = this.PageCount;
if (this.PageIndex < 1) this.PageIndex = 1;
var list = source.Skip(this.PageSize * (this.PageIndex - 1)).Take(this.PageSize);
this.AddRange(list);
}
}
}
这是分页类
使用方法

var Query = from t in context.DB_Users
select t;
Query = Query.OrderByDescending(p => p.UserID);
var list = new Page(Query, Page, PageSize);

翻页类

/// 
/// 显示分页信息
/// 
///
每页多少条
///
当前页
///
总页数
///
总计路数
///
记录单位
///
记录名称
///
翻页函数javascirpt
///
是否显示跳转按钮
/// 
public static string ShowPages(int PageSize, int PageIndex, int PageCount, int AllCount, string Unit, string ItemName, bool JumpPageButton)
{
string tempstr = "";
int jump = PageIndex + 1;
tempstr += "
共  " + AllCount + " " + Unit + ItemName + " ";
tempstr += "  " + PageSize + "条/页 ";
tempstr += " 页次: " + PageIndex + "/" + PageCount + "页 ";
if (PageIndex < 2)
{
tempstr += " 首页  上一页 ";
}
else
{
tempstr += " 首页 ";
tempstr += " 上一页 ";
}
if (PageIndex >= PageCount)
{
tempstr += " 下一页  尾页";
jump = PageCount;
}
else
{
tempstr += " 下一页 ";
tempstr += " 尾页 ";
}
tempstr += " ";
if (JumpPageButton)
{
tempstr += " ";
tempstr += "  跳转";
if (PageCount == 1)
{
tempstr += "
}
else
{
tempstr += "
}
}
tempstr += "
";
return tempstr;
}
使用方法
ShowPages(PageSize, Page, list.PageCount, list.AllCount, "个", "用户", true)

转载于:https://www.cnblogs.com/ziye16888/archive/2013/05/22/3092104.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值