绑定路由的MVC2分页扩展

  许久没写博客,结果越变越懒。杯具到,一年一篇博文的惨淡下场。以后还是要多写点东西,不然别人以为你那个啥。。呵呵

 

  年初玩了一下MVC,还是因为懒,没下MVC3,直接用自带的MVC2。哪个版本更这分页扩展没多大关系,这边共享一下这个分页的扩展。在写之前看了下,他们都是request,页面参数。实在不美观。so..果断改之

 

View Code
  1 /*******************************************************************
  2 
  3  * * Copyright (C) Kurodo
  4 
  5  * * All rights reserved.
  6 
  7  * *
  8 
  9  * Author:   Kurodo
 10 
 11  * Create Date:2011-3-18 8:55:54
 12 
 13  * Description:PagerExtensions.cs
 14 
 15  * *
 16 
 17  * * Date         Author               Description
 18 
 19  * *****************************************************************/
 20 
 21 using System;
 22 using System.Collections.Generic;
 23 using System.Linq;
 24 using System.Web;
 25 using System.Web.Mvc;
 26 using System.Web.Routing;
 27 using System.Web.Mvc.Html;
 28 using System.Text;
 29 
 30 namespace Kurodo.Extensions
 31 {
 32     /// <summary>
 33     /// 分页扩展
 34     /// </summary>
 35     public static class PagerExtensions
 36     {
 37         /// <summary>
 38         /// 分页Pager显示             
 39         /// </summary>              
 40         /// <param name="html"></param>             
 41         /// <param name="keyWord">标识当前页码的参数</param>              
 42         /// <param name="pageSize">每页显示</param>             
 43         /// <param name="totalCount">总数据量</param>             
 44         /// <returns></returns>
 45         public static MvcHtmlString Pager(this HtmlHelper html, string keyWord, int pageSize, int totalCount)
 46         {
 47             int currentPage = 1//当前页
 48             var totalPages = Math.Max((totalCount + pageSize - 1/ pageSize, 1); //总页数
 49             var routeMap = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
 50             var output = new System.Text.StringBuilder();
 51 
 52             if (routeMap[keyWord] != null)
 53             {
 54                 int.TryParse(routeMap[keyWord].ToString(), out currentPage);
 55             }
 56 
 57             if (currentPage <= 0)
 58                 currentPage = 1;
 59             if (totalPages > 1)
 60             {
 61                 if (currentPage != 1)
 62                 {
 63                     //处理首页连接
 64                     routeMap[keyWord] = 1;
 65                     output.AppendFormat("{0} ", html.RouteLink("首页", routeMap));
 66                 }
 67                 if (currentPage > 1)
 68                 {
 69                     //处理上一页的连接
 70                     routeMap[keyWord] = currentPage - 1;
 71                     output.Append(html.RouteLink("上一页", routeMap));
 72                 }
 73                 else
 74                 {
 75                     output.Append("上一页");
 76                 }
 77                 output.Append(" ");
 78                 int currint = 5;
 79                 for (int i = 0; i <= 10; i++)
 80                 {
 81                     //一共最多显示10个页码,前面5个,后面5个
 82                     if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
 83                         if (currint == i)
 84                         {
 85                             //当前页处理
 86                             output.Append(string.Format("<span class\"now\">{0}</span>", currentPage));
 87                         }
 88                         else
 89                         {
 90                             //一般页处理
 91                             routeMap[keyWord] = currentPage + i - currint;
 92                             output.Append(html.RouteLink((currentPage + i - currint).ToString(), routeMap));
 93                         }
 94                     output.Append(" ");
 95                 }
 96 
 97                 if (currentPage < totalPages)
 98                 {
 99                     //处理下一页的链接
100                     routeMap[keyWord] = currentPage + 1;
101                     output.Append(html.RouteLink("下一页", routeMap));
102                 }
103                 else
104                 {
105                     output.Append("下一页");
106                 }
107                 output.Append(" ");
108                 if (currentPage != totalPages)
109                 {
110                     routeMap[keyWord] = totalPages;
111                     output.Append(html.RouteLink("末页", routeMap));
112                 }
113                 output.Append(" ");
114             }
115             output.AppendFormat("{0} / {1}", currentPage, totalPages);//这个统计加不加都行
116 
117             return MvcHtmlString.Create(output.ToString());
118         }
119     }
120 }

 

然后在视图上这样调用即可

 

<%:Html.Pager("page",20,TotalCount) %>

 

如果你是MVC3,就是这样

 

@Html.Pager("page",20,TotalCount)

 

 

posted @ 2011-07-13 13:08  Kurodo  阅读(559)  评论(1编辑  收藏  举报