ASP.NET MVC 分页之HtmlHelper
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;
}
ASP.NET MVC 分页之HtmlHelper的更多相关文章
- ASP.NET MVC分页组件MvcPager 2.0版发布暨网站全新改版
MvcPager分页控件是在ASP.NET MVC Web应用程序中实现分页功能的一系列扩展方法,该分页控件的最初的实现方法借鉴了网上流行的部分源代码, 尤其是ScottGu的PagedList< ...
- ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了 ...
- 基于Bootstrap的Asp.net Mvc 分页
基于Bootstrap的Asp.net Mvc 分页的实现 最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一 ...
- Asp.net MVC分页实例
分页是网页基本功能,这里主要讨论在Asp.net MVC环境下分页的前端实现,不涉及后台分页.实现效果如下图显示: Step 1.建立分页信息类 public class PagingInfo { p ...
- ASP.NET MVC分页实现
ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FO ...
- 自己用的一个ASP.Net MVC分页拿出来分享下(转)
实例懒得做.切几个图把代码发上要用的自己搞啦~ 下面是一个helper类. namespace System.Web.Mvc { public enum BarStyle { yahoo, digg, ...
- asp.net MVC分页
.Net MVC 分页代码,分页的关键就是在于这几个参数pageIndex ,recordCount,pageSize ,下面是张林的网站做的一个简单的分页代码 效果如图 public class ...
- 【asp.net mvc】 扩展 htmlhelper 实现分页
参考文档:http://www.cnblogs.com/caofangsheng/p/5670071.html http://www.cnblogs.com/arte ...
- Asp.Net MVC 分页、检索、排序整体实现
很多时候需要这样的功能,对表格进行分页.排序和检索.这个有很多实现的方式,有现成的表格控件.用前端的mvvm,用户控件.但很多时候看着很漂亮的东西你想进一步控制的时候却不那么如意.这里自己实现一次,功 ...
随机推荐
- Microsoft SQL Server(sql server 关系型数据库管理系统)
sql server一般指Microsoft SQL Server 关系型数据库管理系统 Microsoft SQL Server 是一个全面的数据库平台,使用集成的商业智能 (BI)工具提供了企业级 ...
- LOJ 2555 「CTSC2018」混合果汁——主席树
题目:https://loj.ac/problem/2555 二分答案,在可以选的果汁中,从价格最小的开始选. 按价格排序,每次可以选的就是一个前缀.对序列建主席树,以价格为角标,维护体积和.体积*价 ...
- 2019 牛客暑期多校 第二场 H Second Large Rectangle (单调栈)
题目:https://ac.nowcoder.com/acm/contest/882/H 题意:一个大的01矩阵,然后现在要求第二大的全一矩阵是多少 思路:在这里我们首先学习一下另一个东西,怎么求直方 ...
- Django中的get()和filter()区别
前言 在django中,我们查询经常用的两个API中,会经常用到get()和filter()两个方法,两者的区别是什么呢? object.get()我们得到的是一个对象,如果在数据库中查不到这个对象或 ...
- 讨论Spring整合Mybatis时一级缓存失效得问题
问题 1.学习测试时发现了一级缓存并没有生效,先看案例: setting配置: <settings> <!-- (1) 提供可控制Mybatis框架运行行为的属性信息 --> ...
- selenium2-java 浏览器不同窗口切换
1,获取浏览器的单个窗口: String parentWindowid = driver.getWindowHandle(); System.out.println( ...
- python- ' % '运算符的用途(非常重要)
%运算符就是用来格式化字符串的. 在字符串内部, %s表示用字符串替换, %d表示用整数替换, 有几个%?占位符,后面就跟几个变量或者值,顺序要对应好. 如果只有一个%?,括号可以省略. 另一种格式化 ...
- Html5 学习笔记 --》css3 学习
在开发任务中最好不要使用前缀 可以设置发散图形 圆形 方形等 边框图片效果: CSS3 变形效果: Css3 3D立体变形: css 设置 CSS3 过度效果: div:hover { backgro ...
- MySQL开启SSL认证,以及简单优化
1.1 MySQL开启SSL认证 #生成一个 CA 私钥 [root@db01 ssl]# openssl genrsa 2048 > ca-key.pem Generating RSA pri ...
- IIS 解决跨域问题
打开 HTTP响应标头 添加如下三条 名称Access-Control-Allow-Origin 值*名称Access-Control-Allow-Headers 值Content-Type,Ac ...