码迷,mamicode.com
首页 > Web开发 > 详细

.net mvc 分页

时间:2019-01-14 10:50:51      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:html   htm   int   +=   else   summary   too   htmlhelp   contex   

 1         public static MvcHtmlString PageControl(this HtmlHelper html, PageData page)
 2         {
 3             if (page == null || page.totalpage == 0)
 4                 return MvcHtmlString.Create(string.Empty);
 5 
 6             page.pageindex = page.pageindex < 1 ? 1 : page.pageindex;
 7             string url = HttpContext.Current.Request.Url.ToString();
 8             if (url.IndexOf(#) > -1)
 9                 url = url.Substring(0, url.IndexOf(#));
10 
11             StringBuilder sb = new StringBuilder();
12             sb.Append("<div class=‘paging-toolbar‘>");
13             sb.Append("<ul class=‘pagination‘>");
14             #region 首页
15             if (page.totalpage > 1)
16                 sb.Append("<li><a href=‘" + GetUrl(url, 1) + "‘>首页</a></li>");
17             #endregion
18 
19             #region 上一页
20             if (page.pageindex > 1)
21                 sb.Append("<li><a href=‘" + GetUrl(url, page.pageindex - 1) + "‘>上一页</a></li>");
22             #endregion
23 
24             #region 中间和当前页
25             var half = page.count % 2 == 0 ? page.count / 2 : page.count / 2 + 1; // 展示页一半
26             var leng = page.count;
27             if (page.count > page.totalpage) leng = page.totalpage;
28 
29             if (page.pageindex > page.totalpage - leng + half)
30             {
31                 for (int i = page.totalpage - leng + 1; i <= page.totalpage; i++)
32                 {
33                     if (i == page.pageindex)
34                     {
35                         sb.Append("<li class=‘active‘><a href=‘" + GetUrl(url, page.pageindex) + "‘>" + page.pageindex + "</a></li>");
36                     }
37                     else
38                     {
39                         sb.Append("<li><a href=‘" + GetUrl(url, i) + "‘>" + i + "</a></li>");
40                     }
41                 }
42             }
43             else
44             {
45                 for (int i = 1; i <= leng; i++)
46                 {
47                     if (i == half)
48                     {
49                         sb.Append("<li class=‘active‘><a href=‘" + GetUrl(url, page.pageindex) + "‘>" + page.pageindex + "</a></li>");
50                     }
51                     else
52                     {
53                         var p = page.pageindex - half + i;
54                         if (p > 0)
55                             sb.Append("<li><a href=‘" + GetUrl(url, p) + "‘>" + p + "</a></li>");
56                         else
57                             leng += 1;
58                     }
59                 }
60             }
64             #endregion
65 
66             #region 下一页
67             if (page.pageindex < page.totalpage)
68                 sb.Append("<li><a href=‘" + GetUrl(url, page.pageindex < 2 ? 2 : page.pageindex + 1) + "‘>下一页</a></li>");
69             #endregion
70 
71             #region 末页
72             if (page.totalpage > 1)
73                 sb.Append("<li><a href=‘" + GetUrl(url, page.totalpage) + "‘>末页</a></li>");
74             #endregion
75 
76             sb.Append("</ul>");
77             sb.Append("</div>");
78 
79             return MvcHtmlString.Create(sb.ToString());
80         }
 1     public class PageData
 2     {
 3         /// <summary>
 4         /// 展示的页数
 5         /// </summary>
 6         public int count { get; set; }
 7 
 8         /// <summary>
 9         /// 当前页
10         /// </summary>
11         public int pageindex { get; set; }
12 
13         /// <summary>
14         /// 总页数
15         /// </summary>
16         public int totalpage { get; set; }
17 
18         /// <summary>
19         /// 总条数
20         /// </summary>
21         public int totalrecord { get; set; }
22     }

 

.net mvc 分页

标签:html   htm   int   +=   else   summary   too   htmlhelp   contex   

原文地址:https://www.cnblogs.com/foggy2330/p/10265247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!