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. JSP前三章错题整理

    A: B: C: D:  web-inf目录中的文件不能被客户端直接访问. 正确答案是 C 您回答的是 D 回答错误 试题分析纠错设为收藏 (选择一项) 14 A: B: C: Tomcat作为Web ...

  2. http协议(六)报文首部

    http请求和响应报文内容比较多,会分为大概四部分更新,最近比较忙,没太多时间整理- - 首先来看看报文结构吧 1.http请求报文 http请求报文由方法.URI.http版本.http首部字段等构 ...

  3. js双层动画幻灯

    js双层动画幻灯 点击下载

  4. Android fragment之间消息传递

    1. 在Fragment里定义一个内部接口, 在Fragment初始化方法里, 把父Activity转换成这个接口, 赋值给成员变量 public class DummyFragment extend ...

  5. 微软职位内部推荐-Senior Software Engineer_Azure

    微软近期Open的职位: Job Title: Senior Software Engineer Location: Shanghai, China Have you ever imagined th ...

  6. Html5 Egret游戏开发 成语大挑战(五)界面切换和数据处理

    经过前面的制作,使用Egret的Wing很快完成了开始界面和选关卡界面,下面通常来说就是游戏界面,但此时界面切换和关卡数据还没有准备好,这次讲解界面的切换和关卡数据的解析.前面多次修改了Main.ts ...

  7. (原创)AD账户误删导致Exchange邮箱被删 莫苦恼

    由于人员变动,离职人员AD账户和邮箱经常要删除.但是在删除AD账户的时候难免会犯错,将在用的用户给删除了,这是个痛苦的事情, 然后你会发现Exchange邮箱也会跟着删除,抓狂了..,还好,幸亏这里进 ...

  8. Hadoop:pig 安装及入门示例

    pig是hadoop的一个子项目,用于简化MapReduce的开发工作,可以用更人性化的脚本方式分析数据. 一.安装 a) 下载 从官网http://pig.apache.org下载最新版本(目前是0 ...

  9. 浅谈设计模式--建造器模式(Builder Pattern)

    建造器模式,是于创建带有大量参数的对象,并避免因参数数量多而产生的一些问题(如状态不一致-JavaBean的setter模式). 如果参数多且有些是必须初始化的,有些是不一定需要初始化的时候,创建对象 ...

  10. Java多线程之Runable与Thread

    Java多线程是Java开发中的基础内容,但是涉及到高并发就有很深的研究可做了. 最近看了下<Java并发实战>,发先有些地方,虽然可以理解,但是自己在应用中很难下手. 所以还是先回顾一下 ...