ASP.NET MVC URL分页控件
效果图:
1、实体
using System;
namespace System.Web.Mvc
{
public class MvcPagerModel
{
public int TotalRecords { get; set; } //页数
public int PageIndex { get; set; }//分页索引
public int PageSize { get; set; }//页码
public bool StatisticHtml { get; set; }//是否显示HTML:string.Concat("<span>Pages: ", pageIndex, " / ", index, "</span>")
public MvcPagerModel()
{
StatisticHtml = true;
}
}
}
namespace System.Web.Mvc
{
public class MvcPagerModel
{
public int TotalRecords { get; set; } //页数
public int PageIndex { get; set; }//分页索引
public int PageSize { get; set; }//页码
public bool StatisticHtml { get; set; }//是否显示HTML:string.Concat("<span>Pages: ", pageIndex, " / ", index, "</span>")
public MvcPagerModel()
{
StatisticHtml = true;
}
}
}
2、分页
public static string MvcPager(this HtmlHelper html, MvcPagerModel data){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, object values){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, string action){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, string action, object values){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, string action, string controller, object values){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, RouteValueDictionary values){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, string action, RouteValueDictionary values)
{
string controllerName = html.ViewContext.RouteData.GetRequiredString("controller");
return MvcPager(html, data, action, controllerName, values);
}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, string action, string controller, RouteValueDictionary routeValueDictionary)
{
RouteValueDictionary routeValueDic = ((routeValueDictionary == null) ? new RouteValueDictionary() : routeValueDictionary);
StringBuilder stringBuilder = new StringBuilder();
int pageIndex = data.PageIndex;//分页索引
int totalRecords = data.TotalRecords;//总条数
int pageSize = data.PageSize;//每页分页条数
int index = CalculateTotalPages(totalRecords, pageSize);//分页数
if (totalRecords > 0)
{
if (!pageIndex.Equals(1))
{
stringBuilder.Append(html.ActionLink("<<", action, routeValueDic));
}
else
{
stringBuilder.Append("<span class=\"current\"><<</span>");
}
}
int lower = pageIndex - 3;
if (lower <= 0)
lower = 1;
int higher = lower + 6;
if (higher >= index)
higher = index;
for (int i = lower; i <= higher; i++)
{
if (!i.Equals(pageIndex))
{
if(i.Equals(1))
{
stringBuilder.Append(html.ActionLink(string.Concat(" ", i.ToString(), " "), action, routeValueDic));
}
else
{
routeValueDic.Add("page", i.ToString());
stringBuilder.Append(html.ActionLink(string.Concat(" ", i.ToString(), " "), action, routeValueDic));
routeValueDic.Remove("page");
}
}
else
{
stringBuilder.Append(string.Concat("<span class=\"current\">", i.ToString(), "</span>"));
}
}
if (totalRecords > 0)
{
if (pageIndex.Equals(index))
{
stringBuilder.Append(string.Concat("<span class=\"current\">>></span>"));
}
else
{
routeValueDic.Add("page", index.ToString());
stringBuilder.Append(html.ActionLink(">>", action, routeValueDic));
routeValueDic.Remove("page");
}
}
if (data.TotalRecords.Equals(0)) data.StatisticHtml = false;
if (data.StatisticHtml)
{
stringBuilder.Append(string.Concat("<span>Pages: ", pageIndex, " / ", index, "</span>"));
}
return stringBuilder.ToString();
}
private static int CalculateTotalPages(int totalRecords, int pageSize)
{
int num = totalRecords / pageSize;//计算
if ((totalRecords % pageSize) > 0)
{
num++;
}
return num;
}
public static int GetPageIndex//获取URL page
{
get
{
int page = RequestExtensions.GetIntQueryString("page");//public static int GetIntQueryString(string Key){int num = 0; try{ num = Convert.ToInt32(HttpContext.Current.Request.QueryString[Key]);}catch{}return num;}
int intValue = 1;
if (page.Equals(0) || page < 0)
{
return intValue;
}
else
{
return page;
}
}
}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, object values){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, string action){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, string action, object values){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, string action, string controller, object values){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, RouteValueDictionary values){}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, string action, RouteValueDictionary values)
{
string controllerName = html.ViewContext.RouteData.GetRequiredString("controller");
return MvcPager(html, data, action, controllerName, values);
}
public static string MvcPager(this HtmlHelper html, MvcPagerModel data, string action, string controller, RouteValueDictionary routeValueDictionary)
{
RouteValueDictionary routeValueDic = ((routeValueDictionary == null) ? new RouteValueDictionary() : routeValueDictionary);
StringBuilder stringBuilder = new StringBuilder();
int pageIndex = data.PageIndex;//分页索引
int totalRecords = data.TotalRecords;//总条数
int pageSize = data.PageSize;//每页分页条数
int index = CalculateTotalPages(totalRecords, pageSize);//分页数
if (totalRecords > 0)
{
if (!pageIndex.Equals(1))
{
stringBuilder.Append(html.ActionLink("<<", action, routeValueDic));
}
else
{
stringBuilder.Append("<span class=\"current\"><<</span>");
}
}
int lower = pageIndex - 3;
if (lower <= 0)
lower = 1;
int higher = lower + 6;
if (higher >= index)
higher = index;
for (int i = lower; i <= higher; i++)
{
if (!i.Equals(pageIndex))
{
if(i.Equals(1))
{
stringBuilder.Append(html.ActionLink(string.Concat(" ", i.ToString(), " "), action, routeValueDic));
}
else
{
routeValueDic.Add("page", i.ToString());
stringBuilder.Append(html.ActionLink(string.Concat(" ", i.ToString(), " "), action, routeValueDic));
routeValueDic.Remove("page");
}
}
else
{
stringBuilder.Append(string.Concat("<span class=\"current\">", i.ToString(), "</span>"));
}
}
if (totalRecords > 0)
{
if (pageIndex.Equals(index))
{
stringBuilder.Append(string.Concat("<span class=\"current\">>></span>"));
}
else
{
routeValueDic.Add("page", index.ToString());
stringBuilder.Append(html.ActionLink(">>", action, routeValueDic));
routeValueDic.Remove("page");
}
}
if (data.TotalRecords.Equals(0)) data.StatisticHtml = false;
if (data.StatisticHtml)
{
stringBuilder.Append(string.Concat("<span>Pages: ", pageIndex, " / ", index, "</span>"));
}
return stringBuilder.ToString();
}
private static int CalculateTotalPages(int totalRecords, int pageSize)
{
int num = totalRecords / pageSize;//计算
if ((totalRecords % pageSize) > 0)
{
num++;
}
return num;
}
public static int GetPageIndex//获取URL page
{
get
{
int page = RequestExtensions.GetIntQueryString("page");//public static int GetIntQueryString(string Key){int num = 0; try{ num = Convert.ToInt32(HttpContext.Current.Request.QueryString[Key]);}catch{}return num;}
int intValue = 1;
if (page.Equals(0) || page < 0)
{
return intValue;
}
else
{
return page;
}
}
}
3、调用
html:<%=Html.MvcPager(ViewData["UserRecordTotalRecords"] as MvcPagerModel,
new { StartDate = RequestExtensions.GetStringQueryString("StartDate"),
EndDate = RequestExtensions.GetStringQueryString("EndDate")})%>
cs:
int pageSize = 19;
int pageIndex = MvcPagerExtensions.GetPageIndex;
int totalRecords = 0;
List<UserRecordContract> result = ×××(×××,ref totalRecords);
MvcPagerModel mvcPagerModel = new MvcPagerModel { PageIndex = pageIndex, PageSize = pageSize, TotalRecords = totalRecords };
ViewData["UserRecordTotalRecords"] = mvcPagerModel;
结: 很简单的一个MVC分页,原理就是获取一个列表的总记录数,然后根据页码进行分页。
作者:拽着胃的牛
本文版权归作者和博客园共有,欢迎转载,未经作者同意必须保留此段声明,请在文章页面明显位置给出原文连接。