using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Web; namespace MvcAppPager.Models
{
public interface IPageOfList
{
long CurrentStart { get; }
int PageIndex { get; set; }
int PageSize { get; set; }
int PageTotal { get; }
long RecordTotal { get; set; }
} public interface IPageOfList<T> : IPageOfList, IList<T>
{ }
public class PageOfList<T>:List<T>,IList<T>,IPageOfList,IPageOfList<T>
{
public PageOfList(IEnumerable<T> items, int pageIndex, int pageSize, long recordTotal)
{
if (items!=null)
AddRange(items);
PageIndex = pageIndex;
PageSize = pageSize;
RecordTotal = recordTotal;
} public PageOfList(int pageSize)
{
if (pageSize <= 0)
{
throw new ArgumentException("页面数据量必须大于0", "页面数据量");
}
}
public int PageIndex { get; set; }
public int PageSize { get; set; } public int PageTotal
{
get
{
//RecordTotal / PageSize 获取能够被布满的页面数,(RecordTotal % PageSize > 0 ? 1 : 0)判断是否有未布满的页面。
return (int)RecordTotal / PageSize + (RecordTotal % PageSize > 0 ? 1 : 0);
}
} public long RecordTotal { get; set; }
/// <summary>
/// 当前页面的记录开始位置
/// </summary>
public long CurrentStart
{
get { return PageIndex * PageSize + 1; }
}
/// <summary>
/// 当前页面的结束位置
/// </summary>
public long CurrentEnd
{
get { return (PageIndex + 1) * PageSize > RecordTotal ? RecordTotal : (PageIndex + 1) * PageSize; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace MvcAppPager.Models
{
public class Order
{
public int ID { get; set; }
public string OrderNo { get; set; }
public decimal WayFee { get; set; }
public string EMS { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAppPager.Models; namespace MvcAppPager.Controllers
{
public class HomeController : Controller
{
List<Order> list=new List<Order>
{
new Order{ID=1,OrderNo="2016050501",WayFee = 20,EMS = "C01111"},
new Order{ID=2,OrderNo="2016050502",WayFee = 20,EMS = "C01112"},
new Order{ID=3,OrderNo="2016050503",WayFee = 20,EMS = "C01113"},
new Order{ID=4,OrderNo="2016050504",WayFee = 20,EMS = "C01114"},
new Order{ID=5,OrderNo="2016050505",WayFee = 20,EMS = "C01115"},
new Order{ID=6,OrderNo="2016050506",WayFee = 20,EMS = "C01116"},
}; private const int PageSize = 2; private int counts; //
// GET: /Home/
public ActionResult Index(int pageIndex=0)
{
counts = list.Count;
list = list.Skip(PageSize * pageIndex).Take(PageSize).ToList();
PageOfList<Order> _ordersList=new PageOfList<Order>(list,pageIndex,PageSize,counts);
return View(_ordersList);
} }
}
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html; namespace MvcAppPager.Models
{
public static class ExtHelper
{
public static MvcHtmlString UIPaging(this HtmlHelper helper,IPageOfList list)
{
StringBuilder sb=new StringBuilder(); if (list == null)
{
return new MvcHtmlString(sb.ToString());
}
//显示记录条数和每页有多少条记录
sb.AppendLine("<div class=\"fenye\">"+string.Format("<span>共{0}条记录,每页{1}条  </span>",list.RecordTotal,list.PageSize)); System.Web.Routing.RouteValueDictionary route = new System.Web.Routing.RouteValueDictionary();
//获取路由字典中的控制器【controller】和【action】
foreach (var key in helper.ViewContext.RouteData.Values.Keys)
{
route[key] = helper.ViewContext.RouteData.Values[key];
}
//获取请求信息
foreach (string key in helper.ViewContext.RequestContext.HttpContext.Request.QueryString)
{
route[key] = helper.ViewContext.RequestContext.HttpContext.Request.QueryString[key];
} if (list.PageIndex <= 0)
{
sb.AppendLine("<a class=\"backpage\" href=\"javascript:void(0);\">上一页</a>");
}
else
{
route["pageIndex"] = list.PageIndex - 1; sb.AppendLine(helper.ActionLink("上一页",route["action"].ToString(),route).ToHtmlString());
} if (list.PageIndex>3)
{
route["pageIndex"] = 0;
sb.AppendLine(helper.ActionLink(@"<b>1</b>", route["action"].ToString(), route).ToHtmlString().Replace("<", "<").Replace(">", ">")); if (list.PageIndex>=5)
{
sb.AppendLine("<a href='#'>..</a>");
}
} for (int i = list.PageIndex-2; i <=list.PageIndex ; i++)
{
if (i < 1)
continue; route["pageIndex"] = i - 1;
sb.AppendLine(helper.ActionLink(@"<b>" + i.ToString() + @"</b>", route["action"].ToString(), route).ToHtmlString().Replace("<", "<").Replace(">", ">"));
} sb.AppendLine(@"<a class='active' href='#'><b>" + (list.PageIndex + 1) + @"</b></a>"); for (int i = list.PageIndex + 2; i < list.PageIndex + 4; i++)
{
if (i>list.PageTotal)
continue;
route["pageIndex"] = i - 1;
sb.AppendLine(helper.ActionLink(@"<b>" + i.ToString() + @"</b>", route["action"].ToString(), route).ToHtmlString().Replace("<", "<").Replace(">", ">"));
} if (list.PageIndex<list.PageTotal-4)
{
if (list.PageIndex<=list.PageTotal-6)
{
sb.AppendLine("<a href='#'>..</a>");
}
route["pageIndex"] = list.PageTotal - 1;
sb.AppendLine(helper.ActionLink(@"<b>" + list.PageTotal.ToString() + @"</b>", route["action"].ToString(), route).ToHtmlString().Replace("<", "<").Replace(">", ">"));
} if (list.PageIndex<list.PageTotal-1)
{
route["pageIndex"] = list.PageIndex + 1;
sb.AppendLine(helper.ActionLink("下一页", route["action"].ToString(), route).ToHtmlString());
}
else
{
sb.AppendLine("<a class=\"nextpage\" href=\"javascript:void(0);\">下一页</a>");
} sb.AppendLine("</div>"); return new MvcHtmlString(sb.ToString());
}
}
}
@model MvcAppPager.Models.PageOfList<MvcAppPager.Models.Order>
@{
Layout = null;
ViewBag.Title = "Index";
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
@Styles.Render("~/Content/page.css")
</head>
<body>
<div id="body" style="width: 400px;float: left">
@using (Html.BeginForm("Index","Home",FormMethod.Get))
{
<table>
<tr>
<th>ID</th>
<th>订单号</th>
<th>运单号</th>
<th>运费</th>
</tr>
@if (Model != null && Model.Count > 0)
{
foreach (var item in Model.ToList())
{
<tr>
<td>@item.ID</td>
<td>@item.OrderNo</td>
<td>@item.EMS</td>
<td>@item.WayFee</td>
</tr>
}
}
</table>
@MvcAppPager.Models.ExtHelper.UIPaging(this.Html,Model)
}
</div>
</body>
</html>
.fenye {
float: right;
} .fenye a,.fenye span,.fenye select {
display: block;
float: left;
margin: 0 2px;
} .fenye a {
background: #fff;
border: 1px solid #d6d6d6;
color: #6f6f6f;
height: 22px;
line-height: 22px;
margin: 0 2px;
padding: 0 0 0 8px;
position: relative;
} .fenye a.nextpage:hover {
background: #fff;
border: 1px solid #ba191b;
color: #000;
} .fenye a.nextpage {
height: 22px;
margin: 0 0 0 2px;
padding: 0px 5px;
} .fenye a.backpage {
background: #fff;
border: 1px solid #d6d6d6;
height: 22px;
margin: 0 2px 0 0;
padding: 0px 5px;
} .fenye a.backpage:hover {
background: url("images/pagebut.gif") no-repeat scroll left top rgba(0, 0, 0, 0);
color: #000;
} .fenye a:hover,.fenye a.active {
background: #c32325;
border: 1px solid #ba191b;
color: #ffffff;
text-decoration: none;
} .fenye a.shenlue {
background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
margin: 0 5px;
padding: 0;
border: none;
} .fenye a.shenlue:hover {
color: #333333;
} .fenye a b {
display: block;
font-size: 12px;
font-weight: normal;
height: 22px;
line-height: 22px;
margin-right: 0;
padding: 0 8px 0 0;
position: relative;
}

最新文章

  1. nfs 配置
  2. AliOS编译安装MyRocks
  3. iOS---NSAutoreleasePool自动释放原理及详解
  4. Handler与Looper,MessageQueue的关系
  5. android xutils
  6. 【翻译】Sencha Touch2.4 The Layout System 布局
  7. TEA加密
  8. java初级开发程序员(第三单元)
  9. 手机微博(weibo.cn)模拟登录及页面解析
  10. 修改MAC地址的方法 破解MAC地址绑定(抄)
  11. Java开发知识之Java的数字处理类Math类
  12. php中的一些不常见的问题foreach/in_array[开发篇]
  13. js的map遍历和array遍历
  14. Python问题:UnboundLocalError: local variable &#39;xxx&#39; referenced before assignment
  15. char 与 varchar 区别
  16. jQuery-切换2
  17. Python之路 - Socket实现远程执行命令
  18. 详解使用flask_paginate进行分页
  19. gcc 动态编译 动态库路径
  20. 新的旅程:NodeJS - 环境篇

热门文章

  1. js策略模式vs状态模式
  2. tarjan复习笔记
  3. Python 进阶_OOP 面向对象编程_类和继承
  4. PHP将mysql数据表转换为excel文件
  5. 调用Consul服务(消费服务)
  6. pycharm内对python文件的模板
  7. go 学习之gorm
  8. 棋盘问题(DFS)&amp; Dungeon Master (BFS)
  9. Aspnetcore下面服务器热更新与配置热加载
  10. db2表