最近淘到的MVC分页Helper

首先:看一下效果图:

PagerHelper.cs

代码
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.Specialized;
4 using System.Linq;
5 using System.Web;
6 using System.Text;
7 using System.Web.Mvc;
8 using System.Web.Routing;
9 using System.Data.Objects.DataClasses;
10 namespace System.Web.Mvc
11 {
12 public static class PagerHelper
13 {
14 ///
15 /// 分页
16 ///
17 ///
18 /// 分页id
19 /// 当前页
20 /// 分页尺寸
21 /// 记录总数
22 /// 分页头标签属性
23 /// 分页样式
24 /// 分页模式
25 ///
26 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className,PageMode mode)
27 {
28 TagBuilder builder = new TagBuilder("table");
29 builder.IdAttributeDotReplacement = "_";
30 builder.GenerateId(id);
31 builder.AddCssClass(className);
32 builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
33 builder.InnerHtml = GetNormalPage(currentPageIndex, pageSize, recordCount,mode);
34 return builder.ToString();
35 }
36 ///
37 /// 分页
38 ///
39 ///
40 /// 分页id
41 /// 当前页
42 /// 分页尺寸
43 /// 记录总数
44 /// 分页样式
45 ///
46 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className)
47 {
48 return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className,PageMode.Normal);
49 }
50 ///
51 /// 分页
52 ///
53 ///
54 /// 分页id
55 /// 当前页
56 /// 分页尺寸
57 /// 记录总数
58 ///
59 public static string Pager(this HtmlHelper helper,string id,int currentPageIndex,int pageSize,int recordCount)
60 {
61 return Pager(helper, id, currentPageIndex, pageSize, recordCount,null);
62 }
63 ///
64 /// 分页
65 ///
66 ///
67 /// 分页id
68 /// 当前页
69 /// 分页尺寸
70 /// 记录总数
71 /// 分页模式
72 ///
73 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,PageMode mode)
74 {
75 return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,mode);
76 }
77 ///
78 /// 分页
79 ///
80 ///
81 /// 分页id
82 /// 当前页
83 /// 分页尺寸
84 /// 记录总数
85 /// 分页样式
86 /// 分页模式
87 ///
88 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,string className, PageMode mode)
89 {
90 return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,className,mode);
91 }
92 ///
93 /// 获取普通分页
94 ///
95 ///
96 ///
97 ///
98 ///
99 private static string GetNormalPage(int currentPageIndex, int pageSize, int recordCount,PageMode mode)
100 {
101 int pageCount = (recordCount%pageSize ==0?recordCount/pageSize:recordCount/pageSize+1);
102 StringBuilder url = new StringBuilder();
103 url.Append(HttpContext.Current.Request.Url.AbsolutePath+"?page={0}");
104 NameValueCollection collection = HttpContext.Current.Request.QueryString;
105 string[] keys = collection.AllKeys;
106 for (int i = 0; i < keys.Length; i++)
107 {
108 if (keys[i].ToLower() != "page")
109 url.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]);
110 }
111 StringBuilder sb = new StringBuilder();
112 sb.Append("");
113 sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页 ", recordCount, pageCount, currentPageIndex);
114 if (currentPageIndex == 1)
115 sb.Append("首页 ");
116 else
117 {
118 string url1 = string.Format(url.ToString(), 1);
119 sb.AppendFormat("首页 ", url1);
120 }
121 if (currentPageIndex > 1)
122 {
123 string url1 = string.Format(url.ToString(), currentPageIndex - 1);
124 sb.AppendFormat("上一页 ", url1);
125 }
126 else
127 sb.Append("上一页 ");
128 if(mode == PageMode.Numeric)
129 sb.Append(GetNumericPage(currentPageIndex,pageSize,recordCount,pageCount,url.ToString()));
130 if (currentPageIndex < pageCount)
131 {
132 string url1 = string.Format(url.ToString(), currentPageIndex+1);
133 sb.AppendFormat("下一页 ", url1);
134 }
135 else
136 sb.Append("下一页 ");
137
138 if (currentPageIndex == pageCount)
139 sb.Append("末页 ");
140 else
141 {
142 string url1 = string.Format(url.ToString(), pageCount);
143 sb.AppendFormat("末页 ", url1);
144 }
145 return sb.ToString();
146 }
147 ///
148 /// 获取数字分页
149 ///
150 ///
151 ///
152 ///
153 ///
154 ///
155 ///
156 private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount,string url)
157 {
158 int k = currentPageIndex / 10;
159 int m = currentPageIndex % 10;
160 StringBuilder sb = new StringBuilder();
161 if (currentPageIndex / 10 == pageCount / 10)
162 {
163 if (m == 0)
164 {
165 k--;
166 m = 10;
167 }
168 else
169 m = pageCount%10;
170 }
171 else
172 m = 10;
173 for (int i = k * 10 + 1; i <= k * 10 + m; i++)
174 {
175 if (i == currentPageIndex)
176 sb.AppendFormat("{0} ", i);
177 else
178 {
179 string url1 = string.Format(url.ToString(), i);
180 sb.AppendFormat("{1} ",url1, i);
181 }
182 }
183
184 return sb.ToString();
185 }
186 }
187 ///
188 /// 分页模式
189 ///
190 public enum PageMode
191 {
192 ///
193 /// 普通分页模式
194 ///
195 Normal,
196 ///
197 /// 普通分页加数字分页
198 ///
199 Numeric
200 }
201 }
202

PagerQuery.cs包含两个属性,

一个是PageInfo实体类属性Pager,包含RecordCount,CurrentPageIndex,PageSize三个属性。

一个是Model EntityList属性。 

代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5
6 namespace System.Web.Mvc
7 {
8 public class PagerQuery<TPager,TEntityList>
9 {
10 public PagerQuery(TPager pager, TEntityList entityList)
11 {
12 this.Pager = pager;
13 this.EntityList = entityList;
14 }
15 public TPager Pager { get; set; }
16 public TEntityList EntityList { get; set; }
17 }
18 }

pageinfo.cs

代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5
6 namespace System.Web.Mvc
7 {
8 public class PagerInfo
9 {
10 public int RecordCount { get; set; }
11
12 public int CurrentPageIndex { get; set; }
13
14 public int PageSize { get; set; }
15 }
16 }

使用示例:

代码
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
NewsList
asp:Content
>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>NewsListh2>

<table>
<tr>
<th>th>
<th>
NoteID
th
>
<th>
Title
th
>
<th>
Author
th
>
<th>
Hit
th
>
<th>
ReplyNum
th
>

tr
>

<% foreach (var item in Model.EntityList) { %>

<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
<%= Html.ActionLink("Details", "NewsDetail", new { noteID=item.NoteID })%>
td
>
<td>
<%= Html.Encode(item.NoteID) %>
td
>
<td>
<%= Html.Encode(item.Title) %>
td
>
<td>
<%= Html.Encode(item.Author)%>
td
>
<td>
<%= Html.Encode(item.Hit)%>
td
>
<td>
<%= Html.Encode(item.ReplyNum)%>
td
>

tr
>

<% } %>

table
>

<p>
<%=Html.Pager("pager",Model.Pager.CurrentPageIndex,Model.Pager.PageSize,Model.Pager.RecordCount,PageMode.Numeric) %>
p
>

asp:Content
>

Controler:

代码
[AcceptVerbs(HttpVerbs.Get)]
2 public ActionResult NewsList(int boardID,int? page)
3 {
4 PagerInfo pager = new PagerInfo();
5 NewsArticleInfo info = new NewsArticleInfo();
6 info.NewsBoard = new NewsBoardInfo();
7 info.NewsBoard.BoardID = boardID;
8 pager.RecordCount = Resolve<INewsBLL>().GetArticleDataList(info, ArticleTypeEnum.Pass);
9 pager.PageSize = 10;
10 pager.CurrentPageIndex = (page!=null?(int)page:1);
11 IList<NewsArticleInfo> result = Resolve<INewsBLL>().GetArticleDataList(pager.CurrentPageIndex, pager.PageSize, ArticleTypeEnum.Pass, info);
12 PagerQuery<PagerInfo, IList<NewsArticleInfo>> query = new PagerQuery<PagerInfo, IList<NewsArticleInfo>>(pager,result);
13 return View(query);
14 }

转自:http://kb.cnblogs.com/page/55798/

原文地址:https://www.cnblogs.com/gaoshuai/p/1755792.html