zoukankan      html  css  js  c++  java
  • MVC3 分页Helper

    MVC3 分页Helper

      利用mvc3实现分页效果。效果图如下:

      

      直接拷代码:

      首页添加一个Helper的类(命名空间为System.Web.Mvc;)。    

      

    public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
    {
    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();
    if (totalPages > 1)
    {
    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a> ", redirectTo, pageSize);
    if (currentPage > 1)
    {//处理上一页的连接
    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a> ", redirectTo, currentPage - 1, pageSize);
    }

    output.Append(" ");
    int currint = 5;
    for (int i = 0; i <= 10; i++)
    {//一共最多显示10个页码,前面5个,后面5个
    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
    {
    if (currint == i)
    {//当前页处理
    output.AppendFormat("<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage, pageSize, currentPage);
    }
    else
    {//一般页处理
    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
    }
    }
    output.Append(" ");
    }
    if (currentPage < totalPages)
    {//处理下一页的链接
    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a> ", redirectTo, currentPage + 1, pageSize);
    }

    output.Append(" ");
    if (currentPage != totalPages)
    {
    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a> ", redirectTo, totalPages, pageSize);
    }
    output.Append(" ");
    }
    output.AppendFormat("<label>第{0}页 / 共{1}页</label>", currentPage, totalPages);//这个统计加不加都行

    return new HtmlString(output.ToString());
    }

    复制代码
     1 public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
     2         {
     3             var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
     4             pageSize = pageSize == 0 ? 3 : pageSize;
     5             var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
     6             var output = new StringBuilder();
     7             if (totalPages > 1)
     8             {                
     9                     output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a> ", redirectTo, pageSize);                
    10                 if (currentPage > 1)
    11                 {//处理上一页的连接
    12                     output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a> ", redirectTo, currentPage - 1, pageSize);
    13                 }                
    14 
    15                 output.Append(" ");
    16                 int currint = 5;
    17                 for (int i = 0; i <= 10; i++)
    18                 {//一共最多显示10个页码,前面5个,后面5个
    19                     if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
    20                     {
    21                         if (currint == i)
    22                         {//当前页处理                            
    23                             output.AppendFormat("<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage, pageSize, currentPage);
    24                         }
    25                         else
    26                         {//一般页处理
    27                             output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
    28                         }
    29                     }
    30                     output.Append(" ");
    31                 }
    32                 if (currentPage < totalPages)
    33                 {//处理下一页的链接
    34                     output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a> ", redirectTo, currentPage + 1, pageSize);
    35                 }
    36                 
    37                 output.Append(" ");
    38                 if (currentPage != totalPages)
    39                 {
    40                     output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a> ", redirectTo, totalPages, pageSize);
    41                 }
    42                 output.Append(" ");
    43             }
    44             output.AppendFormat("<label>第{0}页 / 共{1}页</label>", currentPage, totalPages);//这个统计加不加都行
    45 
    46             return new HtmlString(output.ToString());
    47         }
    复制代码

      其次再添加两个公共类:PagerInfo与PageQuery。PagerInfo类用于放置分页相关内容。PageQuery则用于放置PagerInfo及要显示的数据信息。

      

    public class PagerInfo
    {
    public int RecordCount { get; set; }

    public int CurrentPageIndex { get; set; }

    public int PageSize { get; set; }
    }

    复制代码
    1 public class PagerInfo
    2     {
    3         public int RecordCount { get; set; }
    4 
    5         public int CurrentPageIndex { get; set; }
    6 
    7         public int PageSize { get; set; }
    8     }
    复制代码
      

    public class PagerQuery<TPager, TEntityList>
    {
    public PagerQuery(TPager pager, TEntityList entityList)
    {
    this.Pager = pager;
    this.EntityList = entityList;
    }
    public TPager Pager { get; set; }
    public TEntityList EntityList { get; set; }
    }

    复制代码
     1  public class PagerQuery<TPager, TEntityList>
     2     {
     3         public PagerQuery(TPager pager, TEntityList entityList)
     4         {
     5             this.Pager = pager;
     6             this.EntityList = entityList;
     7         }
     8         public TPager Pager { get; set; }
     9         public TEntityList EntityList { get; set; }
    10     }
    复制代码

      然后在Controller里面添加Action

      

    public ActionResult Index(int? pageSize,int? pageIndex)
    {
    int pageIndex1 = pageIndex ?? 1;
    int pageSize1 = pageSize ?? 5;
    int count=0;
    //从数据库在取得数据,并返回总记录数
    var temp = newsSer.LoadPageEntities(c => true, c => c.id, false, pageSize1, pageIndex1, out count);
    PagerInfo pager = new PagerInfo();
    pager.CurrentPageIndex = pageIndex1;
    pager.PageSize = pageSize1;
    pager.RecordCount = count;
    PagerQuery<PagerInfo,IQueryable<news>> query = new PagerQuery<PagerInfo,IQueryable<news>>(pager,temp);
    return View(query);
    }

    复制代码
     1 public ActionResult Index(int? pageSize,int? pageIndex)
     2         {
     3             int pageIndex1 = pageIndex ?? 1;
     4             int pageSize1 = pageSize ?? 5;
     5             int count=0;
     6             //从数据库在取得数据,并返回总记录数
     7             var temp = newsSer.LoadPageEntities(c => true, c => c.id, false, pageSize1, pageIndex1, out count);
     8             PagerInfo pager = new PagerInfo();
     9             pager.CurrentPageIndex = pageIndex1;
    10             pager.PageSize = pageSize1;
    11             pager.RecordCount = count;
    12             PagerQuery<PagerInfo,IQueryable<news>> query = new PagerQuery<PagerInfo,IQueryable<news>>(pager,temp);
    13             return View(query);
    14         }
    复制代码

      最要在View里的部分代码如下:

      @model PagerQuery<PagerInfo, IQueryable<_6_16DFAZ_Models.news>>

      最后在body里面要显示的数据如下:  

    复制代码
     1  <tbody>
     2                         @foreach (var item in Model.EntityList)
     3                         { 
     4                             <tr>
     5                                 <td class="checkBox">
     6                                     <input name="ids[]" type="checkbox" value="" />
     7                                 </td>
     8                                 <td>
     9                                     @item.author
    10                                 </td>
    11                                 <td>
    12                                     @item.title
    13                                 </td>
    14                                 <td>
    15                                     @item.ctime
    16                                 </td>
    17                                 <td>
    18                                     @Html.ActionLink("编辑", "Edit", new { id = item.id }) |
    19                                     @Html.ActionLink("删除", "Delete", new { id = item.id })
    20                                 </td>
    21                             </tr>
    22                         }
    23                         @*分页*@
    24                         <tr class="">
    25                             <td colspan="5" align="center" class="paginator">
    26                                 <span>
    27                                     @Html.ShowPageNavigate(Model.Pager.CurrentPageIndex, Model.Pager.PageSize, Model.Pager.RecordCount)
    28                                 </span>
    29                             </td>
    30                         </tr>
    31                     </tbody>
    复制代码

      ok.分页效果已经实现。为了美观,再添加一些样式。  

      

    .paginator
    {
    font: 12px Arial, Helvetica, sans-serif;
    padding: 10px 20px 10px 0;
    margin: 0px auto;
    }

    .paginator a
    {
    border: solid 1px #ccc;
    color: #0063dc;
    cursor: pointer;
    text-decoration: none;
    }

    .paginator a:visited
    {
    padding: 1px 6px;
    border: solid 1px #ddd;
    background: #fff;
    text-decoration: none;
    }

    .paginator .cpb
    {
    border: 1px solid #F50;
    font-weight: 700;
    color: #F50;
    background-color: #ffeee5;
    }

    .paginator a:hover
    {
    border: solid 1px #F50;
    color: #f60;
    text-decoration: none;
    }

    .paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover
    {
    float: left;
    height: 16px;
    line-height: 16px;
    min- 10px;
    _ 10px;
    margin-right: 5px;
    text-align: center;
    white-space: nowrap;
    font-size: 12px;
    font-family: Arial,SimSun;
    padding: 0 3px;
    }

    .paginator label
    {
    display:block;
    float:left;
    }

    复制代码
     1 .paginator
     2 {
     3     font: 12px Arial, Helvetica, sans-serif;
     4     padding: 10px 20px 10px 0;
     5     margin: 0px auto;
     6 }
     7 
     8 .paginator a
     9 {
    10     border: solid 1px #ccc;
    11     color: #0063dc;
    12     cursor: pointer;
    13     text-decoration: none;
    14 }
    15 
    16 .paginator a:visited
    17 {
    18     padding: 1px 6px;
    19     border: solid 1px #ddd;
    20     background: #fff;
    21     text-decoration: none;
    22 }
    23 
    24 .paginator .cpb
    25 {
    26     border: 1px solid #F50;
    27     font-weight: 700;
    28     color: #F50;
    29     background-color: #ffeee5;
    30 }
    31 
    32 .paginator a:hover
    33 {
    34     border: solid 1px #F50;
    35     color: #f60;
    36     text-decoration: none;
    37 }
    38 
    39 .paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover
    40 {
    41     float: left;
    42     height: 16px;
    43     line-height: 16px;
    44     min-width: 10px;
    45     _width: 10px;
    46     margin-right: 5px;
    47     text-align: center;
    48     white-space: nowrap;
    49     font-size: 12px;
    50     font-family: Arial,SimSun;
    51     padding: 0 3px;
    52 }
    53 
    54 .paginator label
    55 {
    56     display:block;    
    57     float:left;    
    58 }
    复制代码

      最终的显示图如下:

      

      大功告成!

        

      

      

     
     
    分类: MVC3
    标签: mvc 分页分页Helper
  • 相关阅读:
    网络记事本第八天
    软件工程第十周总结
    网络记事本第六,七天
    网络记事本开发,第四天
    网络记事本开发第二,三天
    leetcode 198 打家劫舍
    leetcode 46 全排列
    设计模式 之 动态代理
    设计模式 之 静态代理
    设计模式 之 桥接模式
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3222360.html
Copyright © 2011-2022 走看看