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. nginx集群报错“upstream”directive is not allow here 错误

    nginx集群报错“upstream”directive is not allow here 错误 搭建了一个服务器, 采用的是nginx + apache(多个) + php + mysql(两个) ...

  2. [No000037]操作系统Operating Systems操作系统历史与硬件概况History of OS & Summaries!

    培根:读史使人明智 操作系统的简史 (1955-1965) 计算机非常昂贵,上古神机IBM7094 ,造价在250万美元以上 计算机使用原则:只专注于计算 批处理操作系统(Batch system) ...

  3. ubuntu下解析udt数据包

    udt是通过udp进行端到端可靠传输的一个协议,有其默认拥塞控制算法. 之前ubuntu下wireshark的版本是1.10,不能直接解析udt数据包[1],升级到最新的2.0.0即可过滤udt数据包 ...

  4. 深入运用js

    1,eval()函数 这个函数是获取参数的字符串,并将其作为js来处理,所以这里就有可能有人用这个来搞破坏(比如注入JS脚本文件等),所以最好的是方法是尽量少用,或者可以用new function() ...

  5. Location of several networks in brain

    Source: Naci, L., et al. (2014). "A common neural code for similar conscious experiences in dif ...

  6. 【转】【MySql】Waiting for table metadata lock原因分析

    MySQL在进行alter table等DDL操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的操作停滞在Wa ...

  7. 启动PPT的时候一直配置vs2013的问题解决

    前几天装了VS2013,结果发现每次启动powerpoint都要配置vs2013,虽然时间花的不多,可我看的就是碍眼,我都想把VS2013卸载来解决了.后来抱着试下的态度竟然在google上找到了解决 ...

  8. 基于LeNet网络的中文验证码识别

    基于LeNet网络的中文验证码识别 由于公司需要进行了中文验证码的图片识别开发,最近一段时间刚忙完上线,好不容易闲下来就继上篇<基于Windows10 x64+visual Studio2013 ...

  9. 端口扫描base

    #coding:utf8 import os import socket import sys def IsOpen(ip,port): s = socket.socket(socket.AF_INE ...

  10. gocode+auto-complete搭建emacs的go语言自动补全功能

    上篇随笔记录了在emacs中使用go-mode和goflymake搭建了go语言的简单编程环境(推送门),今天来记录一下使用gocode+auto-complete配置emacs中go语言的自动补全功 ...