在这里我们将谈到的是ASP.NET MVC框架中的一个HTML辅助类,希望通过本文能对大家了解ASP.NET MVC分页有所帮助。
本人写的一个ASP.NET MVC分页Helper,支持普通分页(也就是,首页、上一页、下一页、末页等),综合分页(普通分页和数字分页的综合)。下面是ASP.NET MVC分页效果:
分页代码:
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 publicstaticclass PagerHelper
13 {
26 publicstaticstring 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 }
46 publicstaticstring 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 }
59 publicstaticstring Pager(this HtmlHelper helper,string id,int currentPageIndex,int pageSize,int recordCount)
60 {
61 return Pager(helper, id, currentPageIndex, pageSize, recordCount,null);
62 }
73 publicstaticstring 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 }
88 publicstaticstring 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 }
93 //获取普通分页
99 privatestaticstring 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.Append(String.Format("总共{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 }
148 //获取数字分页
156 privatestaticstring 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 }
188 //分页模式
190 publicenum PageMode
191 {
193 //普通分页模式
195 Normal,
197 //普通分页加数字分页
199 Numeric
200 }
201 }
202
PagerQuery.cs包含两个属性,一个是PageInfo实体类属性Pager,包含RecordCount,CurrentPageIndex,PageSize三个属性。一个是Model EntityList属性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace System.Web.Mvc
{
publicclass PagerQuery<TPager,TEntityList>
{
public PagerQuery(TPager pager, TEntityList entityList)
{
this.Pager = pager;
this.EntityList = entityList;
}
public TPager Pager { get; set; }
public TEntityList EntityList { get; set; }
}
}
PageInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace System.Web.Mvc
{
publicclass PagerInfo
{
publicint RecordCount { get; set; }
publicint CurrentPageIndex { get; set; }
publicint PageSize { get; set; }
}
}
使用示例:
<%@ Page Title=""Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<PagerQuery<PagerInfo, IList<NewsArticleInfo>>>" %>
<asp:ContentID="Content1"ContentPlaceHolderID="TitleContent"runat="server">
NewsList
</asp:Content>
<asp:ContentID="Content2"ContentPlaceHolderID="MainContent"runat="server">
<h2>NewsList</h2>
<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>
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult NewsList(int boardID,int? page)
{
PagerInfo pager = new PagerInfo();
NewsArticleInfo info = new NewsArticleInfo();
info.NewsBoard = new NewsBoardInfo();
info.NewsBoard.BoardID = boardID;
pager.RecordCount = Resolve().GetArticleDataList(info, ArticleTypeEnum.Pass);
pager.PageSize = 10;
pager.CurrentPageIndex = (page!=null?(int)page:1);
IList result = Resolve().GetArticleDataList(pager.CurrentPageIndex, pager.PageSize, ArticleTypeEnum.Pass, info);
PagerQuery> query = new PagerQuery>(pager,result);
return View(query);
}
r