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. class 函数

    cocos2d-x 3.3 lua 版本的class函数用法: local FightScene = class("FightScene", function() return c ...

  2. Convert.ToDateTime(值),方法可以把一个值转化成DateTime类型。

    例子:将日历控件的值转化成DateTime类型. DateTime beginDate = Convert.ToDateTime(this.beginCalendar.EditValue);

  3. 获取iTextSharp 的image 报错

    获取itextsharp类库的image对象的时候报错  outofmemory  .经过艰苦的测试发现jpeg类型是可行的的 iTextSharp.text.Image je = iTextShar ...

  4. top状态及其常用技巧

    看tcp状态 /bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}'   在 top 状态下,按 "shif ...

  5. Android开篇(转)

    转自:http://gityuan.com/android/ 一.简述 Android系统非常庞大.错中复杂,其底层是采用Linux作为基底,上层采用包含虚拟机的Java层以及Native层,通过系统 ...

  6. Asp.net,C# 纯数字加密解密字符串

    也就是说加密后的数据不再是:N8lAaHMFtSAQgaf3+RUFng== 希望encryptedString是"1203877893704809384098328409234923840 ...

  7. 制作stick侧边栏导航效果

    其实这种效果网上好多的插件,但是我想自己实现看看,其实把思路理清实现起来就非常简单了,让我们看看: 分析: 从图中我们可以看出:右边的top=100px,那么它应该在第一个框的top+height=1 ...

  8. 常用hadoop web

    http://localhost:50070 Hadoop服务 http://localhost:8088/ 集群中的所有应用程序 http://localhost:16010 hbase

  9. Scala函数式编程进阶

    package com.dtspark.scala.basics /** * 函数式编程进阶: * 1,函数和变量一样作为Scala语言的一等公民,函数可以直接赋值给变量: * 2, 函数更长用的方式 ...

  10. DEV winform treelist设置背景图像

    treelist是一个复杂的控件,包括选中行,奇偶行等均可以单独设置显示效果,空白区域上背景图像的代码如下: private void treeList1_CustomDrawEmptyArea(ob ...