1: /// <summary>
2: /// 分页Pager显示
3: /// </summary>
4: /// <param name="html"></param>
5: /// <param name="currentPageStr">标识当前页码的QueryStringKey</param>
6: /// <param name="pageSize">每页显示</param>
7: /// <param name="totalCount">总数据量</param>
8: /// <returns></returns>
9: public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
10: {
11: var queryString = html.ViewContext.HttpContext.Request.QueryString;
12: int currentPage = 1; //当前页
13: int.TryParse(queryString[currentPageStr], out currentPage); //与相应的QueryString绑定
14: var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
15: var dict = new RouteValueDictionary(html.ViewContext.RouteData.Values);
16:
17: var output = new StringBuilder();
18:
19: foreach (string key in queryString.Keys)
20: if (queryString[key] != null && !string.IsNullOrEmpty(key))
21: dict[key] = queryString[key];
22: if (totalPages > 1)
23: {
24: if (currentPage != 1)
25: {//处理首页连接
26: dict["p"] = 1;
27: output.AppendFormat("{0} ",html.RouteLink("首页", dict));
28: }
29: if (currentPage > 1)
30: {//处理上一页的连接
31: dict["p"] = currentPage - 1;
32: output.Append(html.RouteLink("上一页", dict));
33: }
34: else
35: {
36: output.Append("上一页");
37: }
38: output.Append(" ");
39: int currint = 5;
40: for (int i = 0; i <= 10; i++)
41: {//一共最多显示10个页码,前面5个,后面5个
42: if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
43: if (currint == i)
44: {//当前页处理
45: output.Append(string.Format("[{0}]", currentPage));
46: }
47: else
48: {//一般页处理
49: dict["p"] = currentPage + i - currint;
50: output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict));
51: }
52: output.Append(" ");
53: }
54: if (currentPage < totalPages)
55: {//处理下一页的链接
56: dict["p"] = currentPage + 1;
57: output.Append(html.RouteLink("下一页", dict));
58: }
59: else
60: {
61: output.Append("下一页");
62: }
63: output.Append(" ");
64: if (currentPage != totalPages)
65: {
66: dict["p"] = totalPages;
67: output.Append(html.RouteLink("末页", dict));
68: }
69: output.Append(" ");
70: }
71: output.AppendFormat("{0} / {1}", currentPage, totalPages);//这个统计加不加都行
72: return output.ToString();
73: }