ASP.NET MVC分頁控件的實(shí)現(xiàn)
需求及模擬代碼
需求,假設(shè)我們有個(gè)列表,有分頁功能,我們可能需要一個(gè)頁碼列表,如
我們模擬寫一下Action:
- public ActionResult Index(int? p)
- {
- if (!p.HasValue) p = 1;//如果未對p傳值就是第1頁
- var list = new List< int>();//生成一個(gè)模擬列表
- for (var i = 0; i < 10;i++ )
- {
- list.Add(p.Value);//是第幾頁就向中填充幾個(gè)這個(gè)頁碼的數(shù)
- }
- return View(list);//強(qiáng)型傳遞給View
View中我寫以下顯示方式:
- < %@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage< List< int>>" %>
- < asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
- Pager for List
- < /asp:Content>
- < asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
- < div>
- < ul>
- < %foreach (int i in Model){//顯示這個(gè)列表%>
- < li>< %=i %>< /li>
- < %} %>
- < /ul>
- < /div>
- < !--將在這里顯示分頁的部分-->
- < /asp:Content>
下面是運(yùn)行后的結(jié)果:
ASP.NET MVC分頁控件:***頁URL類似/Home/Index?p=1
ASP.NET MVC分頁控件:第二頁URL類似/Home/Index?p=2
其它頁面以此類推
ASP.NET MVC分頁控件:最簡單的解決方案
我想最簡單無非就是直接寫鏈接,當(dāng)然也要考慮更換Routing規(guī)則的問題,所以我們可以最簡單如下來寫:
- < %
- int p = 1;
- int.TryParse(Request.QueryString["p"], out p);
- %>
- < div>
- < %=Html.ActionLink("上一頁", "Index", new { p= p-1})%>
- < strong>當(dāng)前頁:< %=p %>< /strong>
- < %=Html.ActionLink("下一頁", "Index", new { p= p+1})%>
- < /div>
這樣就可以得到如果下的分頁樣式
當(dāng)然,也可以根據(jù)這個(gè)來寫1,2,3,4,5頁的鏈接,而不寫“上一頁”或“下一頁”
但是這種方法有個(gè)問題,就是使用Html.ActionLink的時(shí)候要用字符串來指定Action和Controller。下面我們來改換另一種方法來實(shí)現(xiàn)
使用RouteLink來實(shí)現(xiàn)
我們使用Html.RouteLink就可以實(shí)現(xiàn)不與Action或Controller的名稱相耦合,例如:
- < %for (int i = 1; i < 10; i++)
- {
- ViewContext.RouteData.Values["p"] = i;//設(shè)置頁碼
- Writer.Write(
- Html.RouteLink(i.ToString(), ViewContext.RouteData.Values)
- );//顯示設(shè)置頁面后的鏈接
- Writer.Write(" ");//連接后顯示個(gè)空格,好看點(diǎn)
- }%>
這個(gè)列表,我們就可以顯示為
完善這個(gè)Pager并封裝成一個(gè)Helper
上面列出了Pager,但是有幾個(gè)問題
1.沒有上下頁
2.沒有指定當(dāng)前頁的特殊顯示
3.每次調(diào)用時(shí)都要寫一次
4.如果QueryString有其它參數(shù)時(shí)無法處理
那我們下面來完善這個(gè)Pager
并將之封裝成一個(gè)Helper
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- using System.Text;
- using System.Web.Mvc.Html;
- namespace MvcApplication2.Helpers
- {
- public static class PagerExtensions
- {
- /**//// < summary>
- /// 分頁P(yáng)ager顯示
- /// < /summary>
- /// < param name="html">< /param>
- /// < param name="currentPageStr">標(biāo)識(shí)當(dāng)前頁碼的QueryStringKey< /param>
- /// < param name="pageSize">每頁顯示< /param>
- /// < param name="totalCount">總數(shù)據(jù)量< /param>
- /// < returns>< /returns>
- public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
- {
- var queryString = html.ViewContext.HttpContext.Request.QueryString;
- int currentPage = 1; //當(dāng)前頁
- var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //總頁數(shù)
- var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
- var output = new System.Text.StringBuilder();
- if (!string.IsNullOrEmpty(queryString[currentPageStr]))
- {
- //與相應(yīng)的QueryString綁定
- foreach (string key in queryString.Keys)
- if (queryString[key] != null && !string.IsNullOrEmpty(key))
- dict[key] = queryString[key];
- int.TryParse(queryString[currentPageStr], out currentPage);
- }
- else
- {
- //獲取 ~/Page/{page number} 的頁號(hào)參數(shù)
- int.TryParse(dict[currentPageStr].ToString(), out currentPage);
- }
- if (currentPage < = 0) currentPage = 1;
- if (totalPages > 1)
- {
- if (currentPage != 1)
- {
- //處理首頁連接
- dict[currentPageStr] = 1;
- output.AppendFormat("{0} ", html.RouteLink("首頁", dict));
- }
- if (currentPage > 1)
- {
- //處理上一頁的連接
- dict[currentPageStr] = currentPage - 1;
- output.Append(html.RouteLink("上一頁", dict));
- }
- else
- {
- output.Append("上一頁");
- }
- output.Append(" ");
- int currint = 5;
- for (int i = 0; i < = 10; i++)
- {
- //一共最多顯示10個(gè)頁碼,前面5個(gè),后面5個(gè)
- if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) < = totalPages)
- if (currint == i)
- {
- //當(dāng)前頁處理
- output.Append(string.Format("[{0}]", currentPage));
- }
- else
- {
- //一般頁處理
- dict[currentPageStr] = currentPage + i - currint;
- output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict));
- }
- output.Append(" ");
- }
- if (currentPage < totalPages)
- {
- //處理下一頁的鏈接
- dict[currentPageStr] = currentPage + 1;
- output.Append(html.RouteLink("下一頁", dict));
- }
- else
- {
- output.Append("下一頁");
- }
- output.Append(" ");
- if (currentPage != totalPages)
- {
- dict[currentPageStr] = totalPages;
- output.Append(html.RouteLink("末頁", dict));
- }
- output.Append(" ");
- }
- output.AppendFormat("{0} / {1}", currentPage, totalPages);//這個(gè)統(tǒng)計(jì)加不加都行
- return output.ToString();
- }
- }
添加Controller代碼:
- public ActionResult Index(int? page)
- {
- if (page == null)
- page = 1;
- return View();
- }
aspx頁面調(diào)用:
- < %=Html.Pager("page", 10, 10020)%>
效果
這回我們算是解決了這個(gè)問題,ASP.NET MVC分頁控件成功構(gòu)建。
【編輯推薦】