ASP.NET MVC4 HtmlHelper扩展类,实现分页功能
1、扩展HtmlHelper类方法ShowPageNavigate
publicstaticHtmlStringShowPageNavigate(thisHtmlHelperhtmlHelper,intcurrentPage,intpageSize,inttotalCount) { varredirectTo=htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath; pageSize=pageSize==0?3:pageSize; vartotalPages=Math.Max((totalCount+pageSize-1)/pageSize,1);//总页数 varoutput=newStringBuilder(); if(totalPages>1) { output.AppendFormat("<aclass='pageLink'href='{0}?pageIndex=1&pageSize={1}'>首页</a>",redirectTo,pageSize); if(currentPage>1) {//处理上一页的连接 output.AppendFormat("<aclass='pageLink'href='{0}?pageIndex={1}&pageSize={2}'>上一页</a>",redirectTo,currentPage-1,pageSize); } output.Append(""); intcurrint=5; for(inti=0;i<=10;i++) {//一共最多显示10个页码,前面5个,后面5个 if((currentPage+i-currint)>=1&&(currentPage+i-currint)<=totalPages) { if(currint==i) {//当前页处理 output.AppendFormat("<aclass='cpb'href='{0}?pageIndex={1}&pageSize={2}'>{3}</a>",redirectTo,currentPage,pageSize,currentPage); } else {//一般页处理 output.AppendFormat("<aclass='pageLink'href='{0}?pageIndex={1}&pageSize={2}'>{3}</a>",redirectTo,currentPage+i-currint,pageSize,currentPage+i-currint); } } output.Append(""); } if(currentPage<totalPages) {//处理下一页的链接 output.AppendFormat("<aclass='pageLink'href='{0}?pageIndex={1}&pageSize={2}'>下一页</a>",redirectTo,currentPage+1,pageSize); } output.Append(""); if(currentPage!=totalPages) { output.AppendFormat("<aclass='pageLink'href='{0}?pageIndex={1}&pageSize={2}'>末页</a>",redirectTo,totalPages,pageSize); } output.Append(""); } output.AppendFormat("<label>第{0}页/共{1}页</label>",currentPage,totalPages);//这个统计加不加都行 returnnewHtmlString(output.ToString()); }
2、添加公共类PagerInfo,PageQuery
publicclassPagerInfo { publicintRecordCount{get;set;} publicintCurrentPageIndex{get;set;} publicintPageSize{get;set;} } publicclassPagerQuery<TPager,TEntityList> { publicPagerQuery(TPagerpager,TEntityListentityList) { this.Pager=pager; this.EntityList=entityList; } publicTPagerPager{get;set;} publicTEntityListEntityList{get;set;} }
3、然后在Controller里面添加Action
publicActionResultIndex(int?pageSize,int?pageIndex) { intpageIndex1=pageIndex??1; intpageSize1=pageSize??5; intcount=0; //从数据库在取得数据,并返回总记录数 vartemp=newsSer.LoadPageEntities(c=>true,c=>c.id,false,pageSize1,pageIndex1,outcount); PagerInfopager=newPagerInfo(); pager.CurrentPageIndex=pageIndex1; pager.PageSize=pageSize1; pager.RecordCount=count; PagerQuery<PagerInfo,IQueryable<news>>query=newPagerQuery<PagerInfo,IQueryable<news>>(pager,temp); returnView(query); }
4、View里的部分代码
<tbody> @foreach(variteminModel.EntityList) { <tr> <tdclass="checkBox"> <inputname="ids[]"type="checkbox"value=""/> </td> <td> @item.author </td> <td> @item.title </td> <td> @item.ctime </td> <td> @Html.ActionLink("编辑","Edit",new{id=item.id})| @Html.ActionLink("删除","Delete",new{id=item.id}) </td> </tr> } @*分页*@ <trclass=""> <tdcolspan="5"align="center"class="paginator"> <span> @Html.ShowPageNavigate(Model.Pager.CurrentPageIndex,Model.Pager.PageSize,Model.Pager.RecordCount) </span> </td> </tr> </tbody>
5、添加一些样式
.paginator { font:12pxArial,Helvetica,sans-serif; padding:10px20px10px0; margin:0pxauto; } .paginatora { border:solid1px#ccc; color:#0063dc; cursor:pointer; text-decoration:none; } .paginatora:visited { padding:1px6px; border:solid1px#ddd; background:#fff; text-decoration:none; } .paginator.cpb { border:1pxsolid#F50; font-weight:700; color:#F50; background-color:#ffeee5; } .paginatora:hover { border:solid1px#F50; color:#f60; text-decoration:none; } .paginatora,.paginatora:visited,.paginator.cpb,.paginatora:hover { float:left; height:16px; line-height:16px; min-width:10px; _width:10px; margin-right:5px; text-align:center; white-space:nowrap; font-size:12px; font-family:Arial,SimSun; padding:03px; } .paginatorlabel { display:block; float:left; }
6.总结
这个案例简单实现了在MVC中快速分页,其实很多开源的项目中都有相关的HtmlHepler的扩展函数,其中也不乏带有分页的扩展,例如著名的开源商城项目nopCommerce,其中有就一个HtmlExtensions.cs扩展类,里面就有关于分页的扩展,人家写的可是相当专业哦,有兴趣的可以研究一下。