1、扩展HtmlHelper类方法ShowPageNavigate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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());
         }

2、添加公共类PagerInfo,PageQuery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class PagerInfo
     {
         public int RecordCount { getset; }
  
         public int CurrentPageIndex { getset; }
  
         public int PageSize { getset; }
     }
 
 
public class PagerQuery<TPager, TEntityList>
    {
        public PagerQuery(TPager pager, TEntityList entityList)
        {
            this.Pager = pager;
            this.EntityList = entityList;
        }
        public TPager Pager { getset; }
        public TEntityList EntityList { getset; }
    }

3、然后在Controller里面添加Action

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
         }

4、View里的部分代码

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

5、添加一些样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
.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;
     
 }
  
 .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-width: 10px;
     _width: 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;   
 }

6、运行,得效果图

ASP.NET MVC 扩展HtmlHelper类方法的更多相关文章

  1. ASP.NET MVC 扩展HtmlHelper类为 js ,css 资源文件添加版本号

    写在前面 在项目部署当中会需要更新 css 文件或 js 等资源文件,为了避免由于浏览器缓存的原因无法加载新的 css 或 js ,一般的做法是在资源文件的后面加上一个版本号来解决,这样浏览器就会去服 ...

  2. ASP.NET MVC扩展库

    很多同学都读过这篇文章吧 ASP.NET MVC中你必须知道的13个扩展点,今天给大家介绍一个ASP.NET MVC的扩展库,主要就是针对这些扩展点进行.这个项目的核心是IOC容器,包括Ninject ...

  3. ASP.NET MVC Razor HtmlHelper扩展和自定义控件

    先看示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using S ...

  4. 17+个ASP.NET MVC扩展点【附源码】

    1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.  在自定义的Http ...

  5. Asp.Net MVC 扩展 Html.ImageFor 方法详解

    背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelper却无法输出一个img,当用脚手架自动生成一些form或表格的时候, ...

  6. 16个ASP.NET MVC扩展点【附源码】

    转载于:http://www.cnblogs.com/wupeiqi/p/3570445.html 1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中! ...

  7. [转]Asp.Net MVC 扩展联想控件

    本文转自:http://www.cnblogs.com/bright-lin/archive/2013/02/06/MVC_SuggestBox.html 在web中,为改善用户体验,我们常会将一些文 ...

  8. [转]Asp.Net MVC使用HtmlHelper渲染,并传递FormCollection参数的陷阱 【转】

    在Asp.Net MVC 1.0编程中,我们经常遇见这样的场景,在新建一个对象时候,通过HtmlHelper的方式在View模型中渲染Html控件,当填写完相关内容后,通过Form把需要新建的内容Po ...

  9. [转]Asp.Net MVC使用HtmlHelper渲染,并传递FormCollection参数的陷阱

    http://www.cnblogs.com/errorif/archive/2012/02/13/2349902.html 在Asp.Net MVC 1.0编程中,我们经常遇见这样的场景,在新建一个 ...

随机推荐

  1. [No000038]操作系统Operating Systems -CPU

    管理CPU ,先要使用CPU… CPU 的工作原理 CPU上电以后发生了什么? 自动的取指 — 执行 CPU 怎么工作? CPU怎么管理? 管理CPU 的最直观方法 设好PC 初值就完事! 看看这样做 ...

  2. 如何自学Android

    看到很多人提问非科班该如何学习编程,其实科班也基本靠自学.有句话叫"师傅领进门修行靠个人",再厉害的老师能教你的东西都是很有限的,真正的修行还是要靠自己.博主本科是数学专业,虽研究 ...

  3. There was an internal API error.的解决办法

    1.当安装应用到4s时,偶尔有些程序会报这个错误,There was an internal API error 解决办法如下: 真机运行下,项目在iOS8.0下运行正常,但是一旦换到iOS7.0和7 ...

  4. IOS

    网上课程:http://www.zhijieketang.com/ edu.51cto.com 码云:http://git.oschina.net/名字 Gesture:https://www.osc ...

  5. 由项目中一个hash2int函数引发的思考

    hash2int /** * 计算一个字符串的md5折算成int返回 * @param type $str * @return type */ function hash2int($str) { $m ...

  6. Broadmann分区

    来源: http://blog.sina.com.cn/s/blog_60a751620100k2hj.html Brodmann areas Name 中文名 Function 1 Somatose ...

  7. 正在编译转换: 未能找到元数据文件 EntityFramework.dll

    错误 1 正在编译转换: 未能找到元数据文件“C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\..\IDE\Enti ...

  8. web standards

    http://www.w3.org/standards/ http://www.webstandards.org/learn/faq/#p213 http://www.w3.org/standards ...

  9. 大圆满的精髓–肯•威尔伯(KEN WILBER)

    作者:肯·威尔伯(Ken Wilber),目前被公认为是“后人本心理学”的最重要的思想家.理论家和发言人,其影响已经跨越了心理学领域,波及哲学和神学领域.由于肯·威尔伯在意识领域的研究极富基础性和开创 ...

  10. 使用Netty绑定一个端口如何分辨出多种类型的DTU的注册包

    一.  背景 项目需要使用Netty和DTU(无线数据传输模块)通信,需要接入多种类型的DTU,每种dtu连接上来之后都首先会发送一个注册报文.需要解析该注册报文来实现: 1. 分辨出是哪种类型的dt ...