溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ASP.NET MVC中怎么實(shí)現(xiàn)分頁效果

發(fā)布時(shí)間:2021-08-11 11:27:44 來源:億速云 閱讀:150 作者:Leah 欄目:編程語言

ASP.NET MVC中怎么實(shí)現(xiàn)分頁效果,很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

ASP.NET MVC中怎么實(shí)現(xiàn)分頁效果

分頁代碼:

PagerHelper.cs

-->  1 using System;
  2 using System.Collections.Generic;
  3 using System.Collections.Specialized;
  4 using System.Linq;
  5 using System.Web;
  6 using System.Text;
  7 using System.Web.Mvc;
  8 using System.Web.Routing;
  9 using System.Data.Objects.DataClasses;
 10 namespace System.Web.Mvc
 11 {
 12     public static class PagerHelper
 13     {
 14         /// 


 15         /// 分頁
 16         /// 
 17         /// 
 18         /// 分頁id
 19         /// 當(dāng)前頁
 20         /// 分頁尺寸
 21         /// 記錄總數(shù)
 22         /// 分頁頭標(biāo)簽屬性
 23         /// 分頁樣式
 24         /// 分頁模式
 25         /// 


 26         public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, 

int pageSize, int recordCount, object htmlAttributes, string className,PageMode mode)
 27         {
 28             TagBuilder builder = new TagBuilder("table");
 29             builder.IdAttributeDotReplacement = "_";
 30             builder.GenerateId(id);
 31             builder.AddCssClass(className);
 32             builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
 33             builder.InnerHtml = GetNormalPage(currentPageIndex, pageSize, recordCount,mode);
 34             return builder.ToString();
 35         }
 36         /// 


 37         /// 分頁
 38         /// 
 39         /// 
 40         /// 分頁id
 41         /// 當(dāng)前頁
 42         /// 分頁尺寸
 43         /// 記錄總數(shù)
 44         /// 分頁樣式
 45         /// 


 46         public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className)
 47         {
 48             return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className,PageMode.Normal);
 49         }
 50         /// 


 51         /// 分頁
 52         /// 
 53         /// 
 54         /// 分頁id
 55         /// 當(dāng)前頁
 56         /// 分頁尺寸
 57         /// 記錄總數(shù)
 58         /// 


 59         public static string Pager(this HtmlHelper helper,string id,int currentPageIndex,int pageSize,int recordCount)
 60         {
 61             return Pager(helper, id, currentPageIndex, pageSize, recordCount,null);
 62         }
 63         /// 


 64         /// 分頁
 65         /// 
 66         /// 
 67         /// 分頁id
 68         /// 當(dāng)前頁
 69         /// 分頁尺寸
 70         /// 記錄總數(shù)
 71         /// 分頁模式
 72         /// 


 73         public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,PageMode mode)
 74         {
 75             return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,mode);
 76         }
 77         /// 


 78         /// 分頁
 79         /// 
 80         /// 
 81         /// 分頁id
 82         /// 當(dāng)前頁
 83         /// 分頁尺寸
 84         /// 記錄總數(shù)
 85         /// 分頁樣式
 86         /// 分頁模式
 87         /// 


 88         public static string Pager(this HtmlHelper helper, string id, i

nt currentPageIndex, int pageSize, int recordCount,string className, PageMode mode)
 89         {
 90             return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,className,mode);
 91         }
 92         /// 


 93         /// 獲取普通分頁
 94         /// 
 95         /// 
 96         /// 
 97         /// 
 98         /// 


 99         private static string GetNormalPage(int currentPageIndex, int pageSize, int recordCount,PageMode mode)
100         {
101             int pageCount = (recordCount%pageSize ==0?recordCount/pageSize:recordCount/pageSize+1);
102             StringBuilder url = new StringBuilder();
103             url.Append(HttpContext.Current.Request.Url.AbsolutePath+"?page={0}");
104             NameValueCollection collection = HttpContext.Current.Request.QueryString;
105             string[] keys = collection.AllKeys;
106             for (int i = 0; i < keys.Length; i++)
107             {
108                 if (keys[i].ToLower() != "page")
109                     url.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]);
110             }
111             StringBuilder sb = new StringBuilder();
112             sb.Append("


 

");
113             sb.AppendFormat("總共{0}條記錄,共{1}頁,當(dāng)前第{2}頁  ", recordCount, pageCount, currentPageIndex);
114             if (currentPageIndex == 1)
115                 sb.Append("首頁 ");
116             else
117             {
118                 string url1 = string.Format(url.ToString(), 1);
119                 sb.AppendFormat("首頁 ", url1);
120             }
121             if (currentPageIndex > 1)
122             {
123                 string url1 = string.Format(url.ToString(), currentPageIndex - 1);
124                 sb.AppendFormat("上一頁 ", url1);
125             }
126             else
127                 sb.Append("上一頁 ");
128             if(mode == PageMode.Numeric)
129                 sb.Append(GetNumericPage(currentPageIndex,pageSize,recordCount,pageCount,url.ToString()));
130             if (currentPageIndex < pageCount)
131             {
132                 string url1 = string.Format(url.ToString(), currentPageIndex+1);
133                 sb.AppendFormat("下一頁 ", url1);
134             }
135             else
136                 sb.Append("下一頁 ");
137 
138             if (currentPageIndex == pageCount)
139                 sb.Append("末頁 ");
140             else
141             {
142                 string url1 = string.Format(url.ToString(), pageCount);
143                 sb.AppendFormat("末頁 ", url1);
144             }
145             return sb.ToString();
146         }
147         /// 


148         /// 獲取數(shù)字分頁
149         /// 
150         /// 
151         /// 
152         /// 
153         /// 
154         /// 
155         /// 


156         private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount,string url)
157         {
158             int k = currentPageIndex / 10;
159             int m = currentPageIndex % 10;
160             StringBuilder sb = new StringBuilder();
161             if (currentPageIndex / 10 == pageCount / 10)
162             {
163                 if (m == 0)
164                 {
165                     k--;
166                     m = 10;
167                 }
168                 else
169                     m = pageCount%10;
170             }
171             else
172                 m = 10;
173             for (int i = k * 10 + 1; i <= k * 10 + m; i++)
174             {
175                 if (i == currentPageIndex)
176                     sb.AppendFormat("{0} ", i);
177                 else
178                 {
179                     string url1 = string.Format(url.ToString(), i);
180                     sb.AppendFormat("{1} ",url1, i);
181                 }
182             }
183             
184             return sb.ToString();
185         }
186     }
187     /// 


188     /// 分頁模式
189     /// 
190     public enum PageMode
191     {
192         /// 


193         /// 普通分頁模式
194         /// 
195         Normal,
196         /// 


197         /// 普通分頁加數(shù)字分頁
198         /// 
199         Numeric
200     }
201 }
202 

PagerQuery.cs包含兩個(gè)屬性,一個(gè)是PageInfo實(shí)體類屬性Pager,包含RecordCount,CurrentPageIndex,PageSize三個(gè)屬性。一個(gè)是Model EntityList屬性。

代碼

 using System;   using System.Collections.Generic;   using System.Linq;   using System.Web;    namespace System.Web.Mvc   {       public class PagerQuery<TPager,TEntityList>       {          public PagerQuery(TPager pager, TEntityList entityList)          {              this.Pager = pager;              this.EntityList = entityList;          }          public TPager Pager { get; set; }          public TEntityList EntityList { get; set; }       }  }

PageInfo.cs

代碼

 using System;    using System.Collections.Generic;    using System.Linq;    using System.Web;  namespace System.Web.Mvc    {        public class PagerInfo        {           public int RecordCount { get; set; }           public int CurrentPageIndex { get; set; }           public int PageSize { get; set; }      }  }

使用示例:

代碼

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PagerQuery<PagerInfo, IList<NewsArticleInfo>>>" %>  <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">     NewsList  </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h3>NewsList</h3>  <table> <tr> <th></th>             <th>NoteID</th>             <th>Title</th>             <th>Author</th>             <th>Hit</th>             <th>ReplyNum</th> </tr> <% foreach (var item in Model.EntityList) { %> <tr><td> <%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |  <%= Html.ActionLink("Details", "NewsDetail", new { noteID=item.NoteID })%> </td><td>                 <%= Html.Encode(item.NoteID) %>             </td> <td><%= Html.Encode(item.Title) %></td> <td><%= Html.Encode(item.Author)%></td> <td><%= Html.Encode(item.Hit)%></td> <td><%= Html.Encode(item.ReplyNum)%></td>  </tr>  <% } %></table> <p> <%=Html.Pager("pager",Model.Pager.CurrentPageIndex,Model.Pager.PageSize,Model.Pager.RecordCount,PageMode.Numeric) %></p>  </asp:Content> controler:[AcceptVerbs(HttpVerbs.Get)]  ublic ActionResult NewsList(int boardID,int? page)  {      PagerInfo pager = new PagerInfo();      NewsArticleInfo info = new NewsArticleInfo();      info.NewsBoard = new NewsBoardInfo();      info.NewsBoard.BoardID = boardID;      pager.RecordCount = Resolve().GetArticleDataList(info, ArticleTypeEnum.Pass);      pager.PageSize = 10;     pager.CurrentPageIndex = (page!=null?(int)page:1);     IList result = Resolve().GetArticleDataList(pager.CurrentPageIndex, pager.PageSize, ArticleTypeEnum.Pass, info);     PagerQuery> query = new PagerQuery>(pager,result);     return View(query);  原文標(biāo)題:一個(gè)MVC分頁Helper鏈接:http://www.cnblogs.com/JackFeng/archive/2010/01/25/JackFeng.html

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI