国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - MVC默認路由實現分頁(PagerExtend.dll下載)

MVC默認路由實現分頁(PagerExtend.dll下載)

2020-03-11 15:08神牛步行3 ASP.NET教程

這篇文章主要介紹了MVC默認路由實現分頁,采用bootstrap的樣式,文末提供了PagerExtend.dll下載地址,感興趣的小伙伴們可以參考一下

這兩天在群里有人咨詢有沒有現成的.net mvc分頁方法,由此寫了一個簡單分頁工具,這里簡單分享下實現思路,代碼,希望能對大家有些幫助,鼓勵大家多造些輪子還是好的。

A.效果(這里用了bootstrap的樣式)

MVC默認路由實現分頁(PagerExtend.dll下載)

B.分析,知識點

a.分頁通常由一下幾個屬性組成(當前頁,總條數,分頁記錄數,路由地址),由此四項基本就能實現分頁了,在加上一個控制樣式的參數

b.各種數字的驗證,計算總頁數(如果總條數和分頁記錄數不能整除,那么最后相除的結果再+1)

c.下一頁和上一下的按鈕是零界點,需要判斷是否是最后一頁或者第一頁來顯示當前頁數的繼續增加或者減小

d.因為需要在cshtml文件中展示分頁的效果,所以需要用到HtmlHelper擴展方法;擴展方法這里簡單說下注意項:

.關鍵詞this

.擴展方法對應的clas必須靜態,該方法本身也是靜態

.擴展方法對應的class后綴一般是Extensions修飾

e.試圖頁面@Html.PageExtend直接調用分頁方法

C.代碼展示

a.分頁方法實現類

?
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
 
namespace PagerExtend
{
 public static class HtmlHelperExtensions
 {
 
  #region 分頁擴展 PageExtend
 
  /// <summary>
  /// 分頁option屬性
  /// </summary>
  public class MoPagerOption
  {
   /// <summary>
   /// 當前頁 必傳
   /// </summary>
   public int CurrentPage { get; set; }
   /// <summary>
   /// 總條數 必傳
   /// </summary>
   public int Total { get; set; }
 
   /// <summary>
   /// 分頁記錄數(每頁條數 默認每頁15條)
   /// </summary>
   public int PageSize { get; set; }
 
   /// <summary>
   /// 路由地址(格式如:/Controller/Action) 默認自動獲取
   /// </summary>
   public string RouteUrl { get; set; }
 
   /// <summary>
   /// 樣式 默認 bootstrap樣式 1
   /// </summary>
   public int StyleNum { get; set; }
  }
 
  /// <summary>
  /// 分頁擴展方法
  /// </summary>
  /// <param name="helper">html試圖</param>
  /// <param name="option">分頁屬性</param>
  /// <returns>html樣式</returns>
  public static MvcHtmlString PageExtend(this HtmlHelper helper, MoPagerOption option)
  {
 
   if (option.PageSize <= 0) { option.PageSize = 15; }
   if (option.CurrentPage <= 0) { option.CurrentPage = 1; }
   if (option.Total <= 0) { return MvcHtmlString.Empty; }
 
   //總頁數
   var totalPage = option.Total / option.PageSize + (option.Total % option.PageSize > 0 ? 1 : 0);
   if (totalPage <= 0) { return MvcHtmlString.Create("分頁異常"); }
   //當前路由地址
   if (string.IsNullOrEmpty(option.RouteUrl))
   {
 
    option.RouteUrl = helper.ViewContext.HttpContext.Request.RawUrl;
    if (!string.IsNullOrEmpty(option.RouteUrl))
    {
 
     var lastIndex = option.RouteUrl.LastIndexOf("/");
     option.RouteUrl = option.RouteUrl.Substring(0, lastIndex);
    }
   }
   option.RouteUrl = option.RouteUrl.TrimEnd('/');
 
   //構造分頁樣式
   var sbPage = new StringBuilder(string.Empty);
   switch (option.StyleNum)
   {
    case 2:
     {
      break;
     }
    default:
     {
      #region 默認樣式
 
      sbPage.Append("<nav>");
      sbPage.Append(" <ul class=\"pagination\">");
      sbPage.AppendFormat("  <li><a href=\"{0}/{1}\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>",
            option.RouteUrl,
            option.CurrentPage - 1 <= 0 ? 1 : option.CurrentPage - 1);
 
      for (int i = 1; i <= totalPage; i++)
      {
 
       sbPage.AppendFormat("  <li {1}><a href=\"{2}/{0}\">{0}</a></li>",
        i,
        i == option.CurrentPage ? "class=\"active\"" : "",
        option.RouteUrl);
 
      }
 
      sbPage.Append("  <li>");
      sbPage.AppendFormat("   <a href=\"{0}/{1}\" aria-label=\"Next\">",
           option.RouteUrl,
           option.CurrentPage + 1 > totalPage ? option.CurrentPage : option.CurrentPage + 1);
      sbPage.Append("    <span aria-hidden=\"true\">»</span>");
      sbPage.Append("   </a>");
      sbPage.Append("  </li>");
      sbPage.Append(" </ul>");
      sbPage.Append("</nav>");
      #endregion
     }
     break;
   }
 
 
   return MvcHtmlString.Create(sbPage.ToString());
  }
  #endregion
 }
}

b.View測試調用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@using PagerExtend
@model IEnumerable<XinSheng.Api.Controllers.MoAirticle>
 
<table>
 Url:@ViewBag.Url
 
 @foreach (var item in Model)
 {
  <tr>
   <td>@item.Title</td>
   <td>@item.Author</td>
   <td>@item.CreateTime</td>
  </tr>
 }
</table>
 
@Html.PageExtend(ViewBag.PagerOption as HtmlHelperExtensions.MoPagerOption)

c.Controller測試

?
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
using PagerExtend;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
 
namespace XinSheng.Api.Controllers
{
 
 [Serializable]
 public class MoAirticle
 {
 
  public string Title { get; set; }
  public string Author { get; set; }
  public DateTime CreateTime { get; set; }
 }
 
 public class HomeController : Controller
 {
 
  public ActionResult Index(int id)
  {
   ViewBag.Title = "測試 分頁";
 
   List<MoAirticle> moAirticles = new List<MoAirticle>();
 
   for (int i = 1; i < 50; i++)
   {
 
    moAirticles.Add(new MoAirticle
    {
     Author = "神牛步行" + i,
     CreateTime = DateTime.Now,
     Title = "博客園之" + i
    });
   }
   ViewBag.Url = Request.RawUrl;
 
   //初始化分頁基礎信息
   var option = new HtmlHelperExtensions.MoPagerOption
   {
 
    CurrentPage = id,
    PageSize = 15,
    Total = moAirticles.Count
   };
   //動態傳遞分頁屬性
   ViewBag.PagerOption = option;
 
   var articles = moAirticles.Skip((option.CurrentPage - 1) * option.PageSize).Take(option.PageSize).ToList();
   return View(articles);
  }
 }
}

D.分頁PagerExtend.dll下載地址:PagerExtend.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美在线视频一区 | 午夜视频在线 | 亚洲第十页 | 爱逼色 | 午夜寂寞少妇aaa片毛片 | 亚洲视频在线观看免费 | 亚洲精品电影在线观看 | 中文字幕视频在线观看 | 亚洲免费网站 | 黄视频在线观看免费 | 日韩精品成人 | 精品一区二区不卡 | 黄色在线观看视频网站 | 国产一区二区三区免费在线观看 | 色香蕉视频| 久久中文精品 | 国产精品视频久久久 | 日韩欧美精品 | 国产精品美女一区 | 日韩av一区二区在线观看 | 夜夜夜久久久 | 亚洲国产精品一区在线 | 亚洲中午字幕 | 精品视频网站 | 亚洲精品9999 | 成人免费xxxxx在线观看 | 久久国产福利 | 精品久久久一区 | 久草在线视频免费播放 | 久久亚洲精品国产精品紫薇 | 青娱乐一区| 国产综合区 | 亚洲男人的天堂网站 | 国产精品久久嫩一区二区免费 | 久久精品国产亚洲一区二区三区 | 免费观看特级毛片 | 玖玖操| 欧美日韩精品一区二区三区 | 亚洲精品影院 | 成人精品国产免费网站 | 亚洲综合精品 |