标签:
分页Html辅助方法
using System.Text; using System.Web; using System.Web.Mvc; namespace System.Web.Mvc { public static class HtmlExtensions { #region 分页Html辅助方法 /// <summary> /// 分页Html辅助方法 /// </summary> /// <param name="htmlHelper"></param> /// <param name="currentPage"></param> /// <param name="pageSize"></param> /// <param name="totalCount"></param> /// <param name="parameterString"></param> /// <returns></returns> public static HtmlString PageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount, string parameterString) { var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath; pageSize = pageSize == 0 ? 3 : pageSize; var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1);//总页数 var output = new StringBuilder(); output.Append("<nav>"); output.Append("<ul class=‘pagination‘>"); string pageSizrWithParameter = string.Empty; if (!string.IsNullOrEmpty(parameterString)) pageSizrWithParameter = pageSize + "&" + parameterString; if (totalPages>1) { output.AppendFormat("<li><a href=‘{0}?pageIndex=1&pageSize={1}‘ aria-label=‘Previous‘><span aria-hidden=‘true‘>«</span></a></li>",redirectTo,pageSizrWithParameter); if (currentPage > 1)//处理上一页连接 output.AppendFormat("<li><a href=‘{0}?pageIndex={1}&pageSize={2}‘>上一页</a></li>",redirectTo,currentPage-1,pageSizrWithParameter); output.Append(""); int currint = 5; for (int i = 0; i < 10; i++) {//一共最多显示10个页码,前面五个后面五个 if ((currentPage+i-currint)>=1 && (currentPage+1-currint)<=totalPages) { if (currint == i)//当前页处理 output.AppendFormat("<li class=‘active‘><a href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a></li>", redirectTo, currentPage, pageSizrWithParameter, currentPage); else//一般页处理 output.AppendFormat("<li><a href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a></li>",redirectTo,currentPage+i-currint,pageSizrWithParameter,currentPage+i-currint); } output.Append(""); } if (currentPage < totalPages)//处理下一页连接 output.AppendFormat("<li><a href=‘{0}?pageIndex={1}&pageSize={2}‘>下一页</a></li>", redirectTo, currentPage + 1, pageSizrWithParameter); output.Append(""); if (currentPage != totalPages) output.AppendFormat("<li><a href=‘{0}?pageIndex={1}&pageSize={2}‘><span aria-hidden=‘true‘>»</span></a></li>", redirectTo, totalPages, pageSizrWithParameter); output.Append(""); } output.Append("</ul>"); output.Append("</nav>"); return new HtmlString(output.ToString()); } #endregion } }
页面相关位置调用
<!--在控制器里面存储的ViewData,totalRecord表示根据添加查询到的数据并返回的条数,parameter表示搜索条件(关键字搜索等等)--> @Html.PageNavigate(int.Parse(ViewData["pageIndex"].ToString()),int.Parse(ViewData["pageSize"].ToString()),int.Parse(ViewData["totalRecord"].ToString()),ViewData["parameter"].ToString())
控制器相关方法实现
例: public Action Moment(int pageIndex=1,int pageSize=20) { int totalRecord =0; List<Aniuge_Moments> lam =MomentsBussiness.GetInstance().GetMoment(pageSize,pageIndex,out totalRecord);//获取分页显示的列表 ViewData["totalRecord"]=totalRecord; ViewData["pageIndex"]=pageIndex; ViewData["pageSize"]=pageSize; #region 生成搜索状态保持数据 StringBuilder sb =new StringBuilder (); foreach(string item in Request.QueryString.Allkeys) { if(!item.Equals("pageIndex")&&!item.Equls("pageSize")) sb.Append(item + "=" +Request.QueryString[item]+"&"); } ViewData["parameter"]=sb.Tostring().Trim("&"); #endregion return View(lam); }
要无刷新分页及更多功能 看“轻量级表格插件Bootstrap Table。拥有强大的支持固定表头、单/复选、排序、分页、搜索及自定义表头等功能。”这便随笔
标签:
原文地址:http://www.cnblogs.com/qingrp-2015930/p/4852349.html