亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MVC默認(rèn)路由實(shí)現(xiàn)分頁(yè)(PagerExtend.dll下載)

 更新時(shí)間:2016年07月26日 09:47:28   作者:神牛步行3  
這篇文章主要介紹了MVC默認(rèn)路由實(shí)現(xiàn)分頁(yè),采用bootstrap的樣式,文末提供了PagerExtend.dll下載地址,感興趣的小伙伴們可以參考一下

這兩天在群里有人咨詢(xún)有沒(méi)有現(xiàn)成的.net mvc分頁(yè)方法,由此寫(xiě)了一個(gè)簡(jiǎn)單分頁(yè)工具,這里簡(jiǎn)單分享下實(shí)現(xiàn)思路,代碼,希望能對(duì)大家有些幫助,鼓勵(lì)大家多造些輪子還是好的。

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

B.分析,知識(shí)點(diǎn)

a.分頁(yè)通常由一下幾個(gè)屬性組成(當(dāng)前頁(yè),總條數(shù),分頁(yè)記錄數(shù),路由地址),由此四項(xiàng)基本就能實(shí)現(xiàn)分頁(yè)了,在加上一個(gè)控制樣式的參數(shù)

b.各種數(shù)字的驗(yàn)證,計(jì)算總頁(yè)數(shù)(如果總條數(shù)和分頁(yè)記錄數(shù)不能整除,那么最后相除的結(jié)果再+1)

c.下一頁(yè)和上一下的按鈕是零界點(diǎn),需要判斷是否是最后一頁(yè)或者第一頁(yè)來(lái)顯示當(dāng)前頁(yè)數(shù)的繼續(xù)增加或者減小

d.因?yàn)樾枰赾shtml文件中展示分頁(yè)的效果,所以需要用到HtmlHelper擴(kuò)展方法;擴(kuò)展方法這里簡(jiǎn)單說(shuō)下注意項(xiàng):

.關(guān)鍵詞this

.擴(kuò)展方法對(duì)應(yīng)的clas必須靜態(tài),該方法本身也是靜態(tài)

.擴(kuò)展方法對(duì)應(yīng)的class后綴一般是Extensions修飾

e.試圖頁(yè)面@Html.PageExtend直接調(diào)用分頁(yè)方法

C.代碼展示

a.分頁(yè)方法實(shí)現(xiàn)類(lèi)

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 分頁(yè)擴(kuò)展 PageExtend

  /// <summary>
  /// 分頁(yè)option屬性
  /// </summary>
  public class MoPagerOption
  {
   /// <summary>
   /// 當(dāng)前頁(yè) 必傳
   /// </summary>
   public int CurrentPage { get; set; }
   /// <summary>
   /// 總條數(shù) 必傳
   /// </summary>
   public int Total { get; set; }

   /// <summary>
   /// 分頁(yè)記錄數(shù)(每頁(yè)條數(shù) 默認(rèn)每頁(yè)15條)
   /// </summary>
   public int PageSize { get; set; }

   /// <summary>
   /// 路由地址(格式如:/Controller/Action) 默認(rèn)自動(dòng)獲取
   /// </summary>
   public string RouteUrl { get; set; }

   /// <summary>
   /// 樣式 默認(rèn) bootstrap樣式 1
   /// </summary>
   public int StyleNum { get; set; }
  }

  /// <summary>
  /// 分頁(yè)擴(kuò)展方法
  /// </summary>
  /// <param name="helper">html試圖</param>
  /// <param name="option">分頁(yè)屬性</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; }

   //總頁(yè)數(shù)
   var totalPage = option.Total / option.PageSize + (option.Total % option.PageSize > 0 ? 1 : 0);
   if (totalPage <= 0) { return MvcHtmlString.Create("分頁(yè)異常"); }
   //當(dāng)前路由地址
   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('/');

   //構(gòu)造分頁(yè)樣式
   var sbPage = new StringBuilder(string.Empty);
   switch (option.StyleNum)
   {
    case 2:
     {
      break;
     }
    default:
     {
      #region 默認(rèn)樣式

      sbPage.Append("<nav>");
      sbPage.Append(" <ul class=\"pagination\">");
      sbPage.AppendFormat("  <li><a href=\"{0}/{1}\" aria-label=\"Previous\"><span aria-hidden=\"true\">&laquo;</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\">&raquo;</span>");
      sbPage.Append("   </a>");
      sbPage.Append("  </li>");
      sbPage.Append(" </ul>");
      sbPage.Append("</nav>");
      #endregion
     }
     break;
   }


   return MvcHtmlString.Create(sbPage.ToString());
  }
  #endregion
 }
}

b.View測(cè)試調(diào)用

@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測(cè)試

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 = "測(cè)試 分頁(yè)";

   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;

   //初始化分頁(yè)基礎(chǔ)信息
   var option = new HtmlHelperExtensions.MoPagerOption
   {

    CurrentPage = id,
    PageSize = 15,
    Total = moAirticles.Count
   };
   //動(dòng)態(tài)傳遞分頁(yè)屬性
   ViewBag.PagerOption = option;

   var articles = moAirticles.Skip((option.CurrentPage - 1) * option.PageSize).Take(option.PageSize).ToList();
   return View(articles);
  }
 }
}

D.分頁(yè)P(yáng)agerExtend.dll下載地址:PagerExtend.rar

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論