1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography.X509Certificates;
  5. using System.Web;
  6.  
  7. namespace MvcAppPager.Models
  8. {
  9. public interface IPageOfList
  10. {
  11. long CurrentStart { get; }
  12. int PageIndex { get; set; }
  13. int PageSize { get; set; }
  14. int PageTotal { get; }
  15. long RecordTotal { get; set; }
  16. }
  17.  
  18. public interface IPageOfList<T> : IPageOfList, IList<T>
  19. {
  20.  
  21. }
  22. public class PageOfList<T>:List<T>,IList<T>,IPageOfList,IPageOfList<T>
  23. {
  24. public PageOfList(IEnumerable<T> items, int pageIndex, int pageSize, long recordTotal)
  25. {
  26. if (items!=null)
  27. AddRange(items);
  28. PageIndex = pageIndex;
  29. PageSize = pageSize;
  30. RecordTotal = recordTotal;
  31. }
  32.  
  33. public PageOfList(int pageSize)
  34. {
  35. if (pageSize <= 0)
  36. {
  37. throw new ArgumentException("页面数据量必须大于0", "页面数据量");
  38. }
  39. }
  40. public int PageIndex { get; set; }
  41. public int PageSize { get; set; }
  42.  
  43. public int PageTotal
  44. {
  45. get
  46. {
  47. //RecordTotal / PageSize 获取能够被布满的页面数,(RecordTotal % PageSize > 0 ? 1 : 0)判断是否有未布满的页面。
  48. return (int)RecordTotal / PageSize + (RecordTotal % PageSize > 0 ? 1 : 0);
  49. }
  50. }
  51.  
  52. public long RecordTotal { get; set; }
  53. /// <summary>
  54. /// 当前页面的记录开始位置
  55. /// </summary>
  56. public long CurrentStart
  57. {
  58. get { return PageIndex * PageSize + 1; }
  59. }
  60. /// <summary>
  61. /// 当前页面的结束位置
  62. /// </summary>
  63. public long CurrentEnd
  64. {
  65. get { return (PageIndex + 1) * PageSize > RecordTotal ? RecordTotal : (PageIndex + 1) * PageSize; }
  66. }
  67. }
  68. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. namespace MvcAppPager.Models
  7. {
  8. public class Order
  9. {
  10. public int ID { get; set; }
  11. public string OrderNo { get; set; }
  12. public decimal WayFee { get; set; }
  13. public string EMS { get; set; }
  14. }
  15. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using MvcAppPager.Models;
  7.  
  8. namespace MvcAppPager.Controllers
  9. {
  10. public class HomeController : Controller
  11. {
  12. List<Order> list=new List<Order>
  13. {
  14. new Order{ID=1,OrderNo="2016050501",WayFee = 20,EMS = "C01111"},
  15. new Order{ID=2,OrderNo="2016050502",WayFee = 20,EMS = "C01112"},
  16. new Order{ID=3,OrderNo="2016050503",WayFee = 20,EMS = "C01113"},
  17. new Order{ID=4,OrderNo="2016050504",WayFee = 20,EMS = "C01114"},
  18. new Order{ID=5,OrderNo="2016050505",WayFee = 20,EMS = "C01115"},
  19. new Order{ID=6,OrderNo="2016050506",WayFee = 20,EMS = "C01116"},
  20. };
  21.  
  22. private const int PageSize = 2;
  23.  
  24. private int counts;
  25.  
  26. //
  27. // GET: /Home/
  28. public ActionResult Index(int pageIndex=0)
  29. {
  30. counts = list.Count;
  31. list = list.Skip(PageSize * pageIndex).Take(PageSize).ToList();
  32. PageOfList<Order> _ordersList=new PageOfList<Order>(list,pageIndex,PageSize,counts);
  33. return View(_ordersList);
  34. }
  35.  
  36. }
  37. }
  1. using System.Text;
  2. using System.Web.Mvc;
  3. using System.Web.Mvc.Html;
  4.  
  5. namespace MvcAppPager.Models
  6. {
  7. public static class ExtHelper
  8. {
  9. public static MvcHtmlString UIPaging(this HtmlHelper helper,IPageOfList list)
  10. {
  11. StringBuilder sb=new StringBuilder();
  12.  
  13. if (list == null)
  14. {
  15. return new MvcHtmlString(sb.ToString());
  16. }
  17. //显示记录条数和每页有多少条记录
  18. sb.AppendLine("<div class=\"fenye\">"+string.Format("<span>共{0}条记录,每页{1}条  </span>",list.RecordTotal,list.PageSize));
  19.  
  20. System.Web.Routing.RouteValueDictionary route = new System.Web.Routing.RouteValueDictionary();
  21. //获取路由字典中的控制器【controller】和【action】
  22. foreach (var key in helper.ViewContext.RouteData.Values.Keys)
  23. {
  24. route[key] = helper.ViewContext.RouteData.Values[key];
  25. }
  26. //获取请求信息
  27. foreach (string key in helper.ViewContext.RequestContext.HttpContext.Request.QueryString)
  28. {
  29. route[key] = helper.ViewContext.RequestContext.HttpContext.Request.QueryString[key];
  30. }
  31.  
  32. if (list.PageIndex <= 0)
  33. {
  34. sb.AppendLine("<a class=\"backpage\" href=\"javascript:void(0);\">上一页</a>");
  35. }
  36. else
  37. {
  38. route["pageIndex"] = list.PageIndex - 1;
  39.  
  40. sb.AppendLine(helper.ActionLink("上一页",route["action"].ToString(),route).ToHtmlString());
  41. }
  42.  
  43. if (list.PageIndex>3)
  44. {
  45. route["pageIndex"] = 0;
  46. sb.AppendLine(helper.ActionLink(@"<b>1</b>", route["action"].ToString(), route).ToHtmlString().Replace("<", "<").Replace(">", ">"));
  47.  
  48. if (list.PageIndex>=5)
  49. {
  50. sb.AppendLine("<a href='#'>..</a>");
  51. }
  52. }
  53.  
  54. for (int i = list.PageIndex-2; i <=list.PageIndex ; i++)
  55. {
  56. if (i < 1)
  57. continue;
  58.  
  59. route["pageIndex"] = i - 1;
  60. sb.AppendLine(helper.ActionLink(@"<b>" + i.ToString() + @"</b>", route["action"].ToString(), route).ToHtmlString().Replace("<", "<").Replace(">", ">"));
  61. }
  62.  
  63. sb.AppendLine(@"<a class='active' href='#'><b>" + (list.PageIndex + 1) + @"</b></a>");
  64.  
  65. for (int i = list.PageIndex + 2; i < list.PageIndex + 4; i++)
  66. {
  67. if (i>list.PageTotal)
  68. continue;
  69. route["pageIndex"] = i - 1;
  70. sb.AppendLine(helper.ActionLink(@"<b>" + i.ToString() + @"</b>", route["action"].ToString(), route).ToHtmlString().Replace("<", "<").Replace(">", ">"));
  71. }
  72.  
  73. if (list.PageIndex<list.PageTotal-4)
  74. {
  75. if (list.PageIndex<=list.PageTotal-6)
  76. {
  77. sb.AppendLine("<a href='#'>..</a>");
  78. }
  79. route["pageIndex"] = list.PageTotal - 1;
  80. sb.AppendLine(helper.ActionLink(@"<b>" + list.PageTotal.ToString() + @"</b>", route["action"].ToString(), route).ToHtmlString().Replace("<", "<").Replace(">", ">"));
  81. }
  82.  
  83. if (list.PageIndex<list.PageTotal-1)
  84. {
  85. route["pageIndex"] = list.PageIndex + 1;
  86. sb.AppendLine(helper.ActionLink("下一页", route["action"].ToString(), route).ToHtmlString());
  87. }
  88. else
  89. {
  90. sb.AppendLine("<a class=\"nextpage\" href=\"javascript:void(0);\">下一页</a>");
  91. }
  92.  
  93. sb.AppendLine("</div>");
  94.  
  95. return new MvcHtmlString(sb.ToString());
  96. }
  97. }
  98. }
  1. @model MvcAppPager.Models.PageOfList<MvcAppPager.Models.Order>
  2. @{
  3. Layout = null;
  4. ViewBag.Title = "Index";
  5. }
  6.  
  7. <!DOCTYPE html>
  8.  
  9. <html>
  10. <head>
  11. <meta name="viewport" content="width=device-width" />
  12. <title>Index</title>
  13. @Styles.Render("~/Content/page.css")
  14. </head>
  15. <body>
  16. <div id="body" style="width: 400px;float: left">
  17. @using (Html.BeginForm("Index","Home",FormMethod.Get))
  18. {
  19. <table>
  20. <tr>
  21. <th>ID</th>
  22. <th>订单号</th>
  23. <th>运单号</th>
  24. <th>运费</th>
  25. </tr>
  26. @if (Model != null && Model.Count > 0)
  27. {
  28. foreach (var item in Model.ToList())
  29. {
  30. <tr>
  31. <td>@item.ID</td>
  32. <td>@item.OrderNo</td>
  33. <td>@item.EMS</td>
  34. <td>@item.WayFee</td>
  35. </tr>
  36. }
  37. }
  38. </table>
  39. @MvcAppPager.Models.ExtHelper.UIPaging(this.Html,Model)
  40. }
  41. </div>
  42. </body>
  43. </html>
  1. .fenye {
  2. float: right;
  3. }
  4.  
  5. .fenye a,.fenye span,.fenye select {
  6. display: block;
  7. float: left;
  8. margin: 0 2px;
  9. }
  10.  
  11. .fenye a {
  12. background: #fff;
  13. border: 1px solid #d6d6d6;
  14. color: #6f6f6f;
  15. height: 22px;
  16. line-height: 22px;
  17. margin: 0 2px;
  18. padding: 0 0 0 8px;
  19. position: relative;
  20. }
  21.  
  22. .fenye a.nextpage:hover {
  23. background: #fff;
  24. border: 1px solid #ba191b;
  25. color: #000;
  26. }
  27.  
  28. .fenye a.nextpage {
  29. height: 22px;
  30. margin: 0 0 0 2px;
  31. padding: 0px 5px;
  32. }
  33.  
  34. .fenye a.backpage {
  35. background: #fff;
  36. border: 1px solid #d6d6d6;
  37. height: 22px;
  38. margin: 0 2px 0 0;
  39. padding: 0px 5px;
  40. }
  41.  
  42. .fenye a.backpage:hover {
  43. background: url("images/pagebut.gif") no-repeat scroll left top rgba(0, 0, 0, 0);
  44. color: #000;
  45. }
  46.  
  47. .fenye a:hover,.fenye a.active {
  48. background: #c32325;
  49. border: 1px solid #ba191b;
  50. color: #ffffff;
  51. text-decoration: none;
  52. }
  53.  
  54. .fenye a.shenlue {
  55. background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
  56. margin: 0 5px;
  57. padding: 0;
  58. border: none;
  59. }
  60.  
  61. .fenye a.shenlue:hover {
  62. color: #333333;
  63. }
  64.  
  65. .fenye a b {
  66. display: block;
  67. font-size: 12px;
  68. font-weight: normal;
  69. height: 22px;
  70. line-height: 22px;
  71. margin-right: 0;
  72. padding: 0 8px 0 0;
  73. position: relative;
  74. }

ASP.NET MVC 分页之HtmlHelper的更多相关文章

  1. ASP.NET MVC分页组件MvcPager 2.0版发布暨网站全新改版

    MvcPager分页控件是在ASP.NET MVC Web应用程序中实现分页功能的一系列扩展方法,该分页控件的最初的实现方法借鉴了网上流行的部分源代码, 尤其是ScottGu的PagedList< ...

  2. ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页

    我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了 ...

  3. 基于Bootstrap的Asp.net Mvc 分页

    基于Bootstrap的Asp.net Mvc 分页的实现 最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一 ...

  4. Asp.net MVC分页实例

    分页是网页基本功能,这里主要讨论在Asp.net MVC环境下分页的前端实现,不涉及后台分页.实现效果如下图显示: Step 1.建立分页信息类 public class PagingInfo { p ...

  5. ASP.NET MVC分页实现

    ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FO ...

  6. 自己用的一个ASP.Net MVC分页拿出来分享下(转)

    实例懒得做.切几个图把代码发上要用的自己搞啦~ 下面是一个helper类. namespace System.Web.Mvc { public enum BarStyle { yahoo, digg, ...

  7. asp.net MVC分页

    .Net MVC  分页代码,分页的关键就是在于这几个参数pageIndex ,recordCount,pageSize ,下面是张林的网站做的一个简单的分页代码 效果如图 public class ...

  8. 【asp.net mvc】 扩展 htmlhelper 实现分页

    参考文档:http://www.cnblogs.com/caofangsheng/p/5670071.html                  http://www.cnblogs.com/arte ...

  9. Asp.Net MVC 分页、检索、排序整体实现

    很多时候需要这样的功能,对表格进行分页.排序和检索.这个有很多实现的方式,有现成的表格控件.用前端的mvvm,用户控件.但很多时候看着很漂亮的东西你想进一步控制的时候却不那么如意.这里自己实现一次,功 ...

随机推荐

  1. 攻防世界 WEB篇

    0x01 ics-06 查看源码发现:index.php 一开始直接用sqlmap跑了下没有发现注入,然后用brupsuite爆破参数 0x02 NewsCenter SQL注入中的POST注入,查阅 ...

  2. [CSP-S模拟测试]:平均数(二分答案+归并排序)

    题目描述 有一天,小$A$得到了一个长度为$n$的序列.他把这个序列的所有连续子序列都列了出来,并对每一个子序列都求了其平均值,然后他把这些平均值写在纸上,并对它们进行排序,最后他报出了第$k$小的平 ...

  3. 数据挖掘之DecisionTreeClassifier决策树

    用决策树DecisionTreeClassifier的数据挖掘算法来通过三个参数,Pclass,Sex,Age,三个参数来求取乘客的获救率. 分为三大步: 一,创建决策树DecisionTreeCla ...

  4. Mac os下设置国内镜像加速站

    无法忍受国外pip 仓库的龟速地址,安利一波国内高速镜像地址... 首推阿里云 repository 马爸爸  I ❤ u $ vim ~/.pip/pip.conf 在config中做如下配置: [ ...

  5. php使用curl抓取网页自动跳转问题处理

    问题分析: 请求抓取http://go.com数据: function curlGet($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, ...

  6. 107、TensorFlow变量(三)

    创建秩为1的张量 # create a rank1 tensor object import tensorflow as tf mystr = tf.Variable(["Hello&quo ...

  7. 小米手机_adb安装apk报错”Failure [INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]“

    问题: adb安装apk至小米手机时,安装失败,报错提示“Failure [INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]”,如下图 ...

  8. haskell目录层次

    daniel@daniel-mint /usr/lib/ghc/haskell2010-1.1.1.0 $ tree . ├── Control │   └── Monad.hi ├── Data │ ...

  9. USACO 5.4 章节

    Canada Tour 题目大意 双向连通图,点从左向右排列, 你需要先从最左的点到最右的点,(过程中只能从左向右走) 然后再从最右的点返回最左的点,(过程中只能从右向左走) 过程中除了最左的点,其它 ...

  10. upc组队赛15 Made In Heaven【第K短路 A*】

    Made In Heaven 题目描述 One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with ...