MVC分页
分页效果如下:
代码
代码
代码
代码
代码
分页代码:
PagerHelper.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Collections.Specialized;
4usingSystem.Linq;
5usingSystem.Web;
6usingSystem.Text;
7usingSystem.Web.Mvc;
8usingSystem.Web.Routing;
9usingSystem.Data.Objects.DataClasses;
10namespaceSystem.Web.Mvc
11{
12publicstaticclassPagerHelper
13{
14///
15///分页
16///
17///
18///分页id
19///当前页
20///分页尺寸
21///记录总数
22///分页头标签属性
23///分页样式
24///分页模式
25///
26publicstaticstringPager(thisHtmlHelperhelper,stringid,intcurrentPageIndex,intpageSize,intrecordCount,objecthtmlAttributes,stringclassName,PageModemode)
27{
28TagBuilderbuilder=newTagBuilder("table");
29builder.IdAttributeDotReplacement="_";
30builder.GenerateId(id);
31builder.AddCssClass(className);
32builder.MergeAttributes(newRouteValueDictionary(htmlAttributes));
33builder.InnerHtml=GetNormalPage(currentPageIndex,pageSize,recordCount,mode);
34returnbuilder.ToString();
35}
36///
37///分页
38///
39///
40///分页id
41///当前页
42///分页尺寸
43///记录总数
44///分页样式
45///
46publicstaticstringPager(thisHtmlHelperhelper,stringid,intcurrentPageIndex,intpageSize,intrecordCount,stringclassName)
47{
48returnPager(helper,id,currentPageIndex,pageSize,recordCount,null,className,PageMode.Normal);
49}
50///
51///分页
52///
53///
54///分页id
55///当前页
56///分页尺寸
57///记录总数
58///
59publicstaticstringPager(thisHtmlHelperhelper,stringid,intcurrentPageIndex,intpageSize,intrecordCount)
60{
61returnPager(helper,id,currentPageIndex,pageSize,recordCount,null);
62}
63///
64///分页
65///
66///
67///分页id
68///当前页
69///分页尺寸
70///记录总数
71///分页模式
72///
73publicstaticstringPager(thisHtmlHelperhelper,stringid,intcurrentPageIndex,intpageSize,intrecordCount,PageModemode)
74{
75returnPager(helper,id,currentPageIndex,pageSize,recordCount,null,mode);
76}
77///
78///分页
79///
80///
81///分页id
82///当前页
83///分页尺寸
84///记录总数
85///分页样式
86///分页模式
87///
88publicstaticstringPager(thisHtmlHelperhelper,stringid,intcurrentPageIndex,intpageSize,intrecordCount,stringclassName,PageModemode)
89{
90returnPager(helper,id,currentPageIndex,pageSize,recordCount,null,className,mode);
91}
92///
93///获取普通分页
94///
95///
96///
97///
98///
99privatestaticstringGetNormalPage(intcurrentPageIndex,intpageSize,intrecordCount,PageModemode)
100{
101intpageCount=(recordCount%pageSize==0?recordCount/pageSize:recordCount/pageSize+1);
102StringBuilderurl=newStringBuilder();
103url.Append(HttpContext.Current.Request.Url.AbsolutePath+"?page={0}");
104NameValueCollectioncollection=HttpContext.Current.Request.QueryString;
105string[]keys=collection.AllKeys;
106for(inti=0;i<keys.Length;i++)
107{
108if(keys[i].ToLower()!="page")
109url.AppendFormat("&{0}={1}",keys[i],collection[keys[i]]);
110}
111StringBuildersb=newStringBuilder();
112sb.Append("");
113sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页",recordCount,pageCount,currentPageIndex);
114if(currentPageIndex==1)
115sb.Append("首页");
116else
117{
118stringurl1=string.Format(url.ToString(),1);
119sb.AppendFormat("首页",url1);
120}
121if(currentPageIndex>1)
122{
123stringurl1=string.Format(url.ToString(),currentPageIndex-1);
124sb.AppendFormat("上一页",url1);
125}
126else
127sb.Append("上一页");
128if(mode==PageMode.Numeric)
129sb.Append(GetNumericPage(currentPageIndex,pageSize,recordCount,pageCount,url.ToString()));
130if(currentPageIndex<pageCount)
131{
132stringurl1=string.Format(url.ToString(),currentPageIndex+1);
133sb.AppendFormat("下一页",url1);
134}
135else
136sb.Append("下一页");
137
138if(currentPageIndex==pageCount)
139sb.Append("末页");
140else
141{
142stringurl1=string.Format(url.ToString(),pageCount);
143sb.AppendFormat("末页",url1);
144}
145returnsb.ToString();
146}
147///
148///获取数字分页
149///
150///
151///
152///
153///
154///
155///
156privatestaticstringGetNumericPage(intcurrentPageIndex,intpageSize,intrecordCount,intpageCount,stringurl)
157{
158intk=currentPageIndex/10;
159intm=currentPageIndex%10;
160StringBuildersb=newStringBuilder();
161if(currentPageIndex/10==pageCount/10)
162{
163if(m==0)
164{
165k--;
166m=10;
167}
168else
169m=pageCount%10;
170}
171else
172m=10;
173for(inti=k*10+1;i<=k*10+m;i++)
174{
175if(i==currentPageIndex)
176sb.AppendFormat("{0}",i);
177else
178{
179stringurl1=string.Format(url.ToString(),i);
180sb.AppendFormat("{1}",url1,i);
181}
182}
183
184returnsb.ToString();
185}
186}
187///
188///分页模式
189///
190publicenumPageMode
191{
192///
193///普通分页模式
194///
195Normal,
196///
197///普通分页加数字分页
198///
199Numeric
200}
201}
202
2usingSystem.Collections.Generic;
3usingSystem.Collections.Specialized;
4usingSystem.Linq;
5usingSystem.Web;
6usingSystem.Text;
7usingSystem.Web.Mvc;
8usingSystem.Web.Routing;
9usingSystem.Data.Objects.DataClasses;
10namespaceSystem.Web.Mvc
11{
12publicstaticclassPagerHelper
13{
14///
15///分页
16///
17///
18///分页id
19///当前页
20///分页尺寸
21///记录总数
22///分页头标签属性
23///分页样式
24///分页模式
25///
26publicstaticstringPager(thisHtmlHelperhelper,stringid,intcurrentPageIndex,intpageSize,intrecordCount,objecthtmlAttributes,stringclassName,PageModemode)
27{
28TagBuilderbuilder=newTagBuilder("table");
29builder.IdAttributeDotReplacement="_";
30builder.GenerateId(id);
31builder.AddCssClass(className);
32builder.MergeAttributes(newRouteValueDictionary(htmlAttributes));
33builder.InnerHtml=GetNormalPage(currentPageIndex,pageSize,recordCount,mode);
34returnbuilder.ToString();
35}
36///
37///分页
38///
39///
40///分页id
41///当前页
42///分页尺寸
43///记录总数
44///分页样式
45///
46publicstaticstringPager(thisHtmlHelperhelper,stringid,intcurrentPageIndex,intpageSize,intrecordCount,stringclassName)
47{
48returnPager(helper,id,currentPageIndex,pageSize,recordCount,null,className,PageMode.Normal);
49}
50///
51///分页
52///
53///
54///分页id
55///当前页
56///分页尺寸
57///记录总数
58///
59publicstaticstringPager(thisHtmlHelperhelper,stringid,intcurrentPageIndex,intpageSize,intrecordCount)
60{
61returnPager(helper,id,currentPageIndex,pageSize,recordCount,null);
62}
63///
64///分页
65///
66///
67///分页id
68///当前页
69///分页尺寸
70///记录总数
71///分页模式
72///
73publicstaticstringPager(thisHtmlHelperhelper,stringid,intcurrentPageIndex,intpageSize,intrecordCount,PageModemode)
74{
75returnPager(helper,id,currentPageIndex,pageSize,recordCount,null,mode);
76}
77///
78///分页
79///
80///
81///分页id
82///当前页
83///分页尺寸
84///记录总数
85///分页样式
86///分页模式
87///
88publicstaticstringPager(thisHtmlHelperhelper,stringid,intcurrentPageIndex,intpageSize,intrecordCount,stringclassName,PageModemode)
89{
90returnPager(helper,id,currentPageIndex,pageSize,recordCount,null,className,mode);
91}
92///
93///获取普通分页
94///
95///
96///
97///
98///
99privatestaticstringGetNormalPage(intcurrentPageIndex,intpageSize,intrecordCount,PageModemode)
100{
101intpageCount=(recordCount%pageSize==0?recordCount/pageSize:recordCount/pageSize+1);
102StringBuilderurl=newStringBuilder();
103url.Append(HttpContext.Current.Request.Url.AbsolutePath+"?page={0}");
104NameValueCollectioncollection=HttpContext.Current.Request.QueryString;
105string[]keys=collection.AllKeys;
106for(inti=0;i<keys.Length;i++)
107{
108if(keys[i].ToLower()!="page")
109url.AppendFormat("&{0}={1}",keys[i],collection[keys[i]]);
110}
111StringBuildersb=newStringBuilder();
112sb.Append("");
113sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页",recordCount,pageCount,currentPageIndex);
114if(currentPageIndex==1)
115sb.Append("首页");
116else
117{
118stringurl1=string.Format(url.ToString(),1);
119sb.AppendFormat("首页",url1);
120}
121if(currentPageIndex>1)
122{
123stringurl1=string.Format(url.ToString(),currentPageIndex-1);
124sb.AppendFormat("上一页",url1);
125}
126else
127sb.Append("上一页");
128if(mode==PageMode.Numeric)
129sb.Append(GetNumericPage(currentPageIndex,pageSize,recordCount,pageCount,url.ToString()));
130if(currentPageIndex<pageCount)
131{
132stringurl1=string.Format(url.ToString(),currentPageIndex+1);
133sb.AppendFormat("下一页",url1);
134}
135else
136sb.Append("下一页");
137
138if(currentPageIndex==pageCount)
139sb.Append("末页");
140else
141{
142stringurl1=string.Format(url.ToString(),pageCount);
143sb.AppendFormat("末页",url1);
144}
145returnsb.ToString();
146}
147///
148///获取数字分页
149///
150///
151///
152///
153///
154///
155///
156privatestaticstringGetNumericPage(intcurrentPageIndex,intpageSize,intrecordCount,intpageCount,stringurl)
157{
158intk=currentPageIndex/10;
159intm=currentPageIndex%10;
160StringBuildersb=newStringBuilder();
161if(currentPageIndex/10==pageCount/10)
162{
163if(m==0)
164{
165k--;
166m=10;
167}
168else
169m=pageCount%10;
170}
171else
172m=10;
173for(inti=k*10+1;i<=k*10+m;i++)
174{
175if(i==currentPageIndex)
176sb.AppendFormat("{0}",i);
177else
178{
179stringurl1=string.Format(url.ToString(),i);
180sb.AppendFormat("{1}",url1,i);
181}
182}
183
184returnsb.ToString();
185}
186}
187///
188///分页模式
189///
190publicenumPageMode
191{
192///
193///普通分页模式
194///
195Normal,
196///
197///普通分页加数字分页
198///
199Numeric
200}
201}
202
PagerQuery.cs包含两个属性,一个是PageInfo实体类属性Pager,包含RecordCount,CurrentPageIndex,PageSize三个属性。一个是Model EntityList属性。
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Web;
5
6namespaceSystem.Web.Mvc
7{
8publicclassPagerQuery<TPager,TEntityList>
9{
10publicPagerQuery(TPagerpager,TEntityListentityList)
11{
12this.Pager=pager;
13this.EntityList=entityList;
14}
15publicTPagerPager{get;set;}
16publicTEntityListEntityList{get;set;}
17}
18}
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Web;
5
6namespaceSystem.Web.Mvc
7{
8publicclassPagerQuery<TPager,TEntityList>
9{
10publicPagerQuery(TPagerpager,TEntityListentityList)
11{
12this.Pager=pager;
13this.EntityList=entityList;
14}
15publicTPagerPager{get;set;}
16publicTEntityListEntityList{get;set;}
17}
18}
PageInfo.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Web;
5
6namespaceSystem.Web.Mvc
7{
8publicclassPagerInfo
9{
10publicintRecordCount{get;set;}
11
12publicintCurrentPageIndex{get;set;}
13
14publicintPageSize{get;set;}
15}
16}
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Web;
5
6namespaceSystem.Web.Mvc
7{
8publicclassPagerInfo
9{
10publicintRecordCount{get;set;}
11
12publicintCurrentPageIndex{get;set;}
13
14publicintPageSize{get;set;}
15}
16}
使用示例:
<%@PageTitle=""Language="C#"MasterPageFile="~/Views/Shared/Site.Master"Inherits="System.Web.Mvc.ViewPage>>"%>
<asp:ContentID="Content1"ContentPlaceHolderID="TitleContent"runat="server">
NewsList
asp:Content>
<asp:ContentID="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(variteminModel.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>
<asp:ContentID="Content1"ContentPlaceHolderID="TitleContent"runat="server">
NewsList
asp:Content>
<asp:ContentID="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(variteminModel.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:
1[AcceptVerbs(HttpVerbs.Get)]
2publicActionResultNewsList(intboardID,int?page)
3{
4PagerInfopager=newPagerInfo();
5NewsArticleInfoinfo=newNewsArticleInfo();
6info.NewsBoard=newNewsBoardInfo();
7info.NewsBoard.BoardID=boardID;
8pager.RecordCount=Resolve<INewsBLL>().GetArticleDataList(info,ArticleTypeEnum.Pass);
9pager.PageSize=10;
10pager.CurrentPageIndex=(page!=null?(int)page:1);
11IList<NewsArticleInfo>result=Resolve<INewsBLL>().GetArticleDataList(pager.CurrentPageIndex,pager.PageSize,ArticleTypeEnum.Pass,info);
12PagerQuery<PagerInfo,IList<NewsArticleInfo>>query=newPagerQuery<PagerInfo,IList<NewsArticleInfo>>(pager,result);
13returnView(query);
14}
2publicActionResultNewsList(intboardID,int?page)
3{
4PagerInfopager=newPagerInfo();
5NewsArticleInfoinfo=newNewsArticleInfo();
6info.NewsBoard=newNewsBoardInfo();
7info.NewsBoard.BoardID=boardID;
8pager.RecordCount=Resolve<INewsBLL>().GetArticleDataList(info,ArticleTypeEnum.Pass);
9pager.PageSize=10;
10pager.CurrentPageIndex=(page!=null?(int)page:1);
11IList<NewsArticleInfo>result=Resolve<INewsBLL>().GetArticleDataList(pager.CurrentPageIndex,pager.PageSize,ArticleTypeEnum.Pass,info);
12PagerQuery<PagerInfo,IList<NewsArticleInfo>>query=newPagerQuery<PagerInfo,IList<NewsArticleInfo>>(pager,result);
13returnView(query);
14}