您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關asp.net MVC怎樣分頁,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體內容如下
using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Web; using System.Text; using System.Web.Mvc; using System.Web.Routing; using System.Data.Objects.DataClasses; namespace System.Web.Mvc { public static class PagerHelper { /// <summary> /// 分頁 /// </summary> /// <param name="helper"></param> /// <param name="id">分頁id</param> /// <param name="currentPageIndex">當前頁</param> /// <param name="pageSize">分頁尺寸</param> /// <param name="recordCount">記錄總數</param> /// <param name="htmlAttributes">分頁頭標簽屬性</param> /// <param name="className">分頁樣式</param> /// <param name="mode">分頁模式</param> /// <returns></returns> public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className, PageMode mode) { TagBuilder builder = new TagBuilder("table"); builder.IdAttributeDotReplacement = "_"; builder.GenerateId(id); builder.AddCssClass(className); builder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); builder.InnerHtml = GetNormalPage(currentPageIndex, pageSize, recordCount, mode); return builder.ToString(); } /// <summary> /// 分頁 /// </summary> /// <param name="helper"></param> /// <param name="id">分頁id</param> /// <param name="currentPageIndex">當前頁</param> /// <param name="pageSize">分頁尺寸</param> /// <param name="recordCount">記錄總數</param> /// <param name="className">分頁樣式</param> /// <returns></returns> public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className) { return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className, PageMode.Normal); } /// <summary> /// 分頁 /// </summary> /// <param name="helper"></param> /// <param name="id">分頁id</param> /// <param name="currentPageIndex">當前頁</param> /// <param name="pageSize">分頁尺寸</param> /// <param name="recordCount">記錄總數</param> /// <returns></returns> public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount) { return Pager(helper, id, currentPageIndex, pageSize, recordCount, null); } /// <summary> /// 分頁 /// </summary> /// <param name="helper"></param> /// <param name="id">分頁id</param> /// <param name="currentPageIndex">當前頁</param> /// <param name="pageSize">分頁尺寸</param> /// <param name="recordCount">記錄總數</param> /// <param name="mode">分頁模式</param> /// <returns></returns> public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, PageMode mode) { return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, mode); } /// <summary> /// 分頁 /// </summary> /// <param name="helper"></param> /// <param name="id">分頁id</param> /// <param name="currentPageIndex">當前頁</param> /// <param name="pageSize">分頁尺寸</param> /// <param name="recordCount">記錄總數</param> /// <param name="className">分頁樣式</param> /// <param name="mode">分頁模式</param> /// <returns></returns> public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className, PageMode mode) { return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className, mode); } /// <summary> /// 獲取普通分頁 /// </summary> /// <param name="currentPageIndex"></param> /// <param name="pageSize"></param> /// <param name="recordCount"></param> /// <returns></returns> private static string GetNormalPage(int currentPageIndex, int pageSize, int recordCount, PageMode mode) { int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1); StringBuilder url = new StringBuilder(); url.Append(HttpContext.Current.Request.Url.AbsolutePath + "?page={0}"); NameValueCollection collection = HttpContext.Current.Request.QueryString; string[] keys = collection.AllKeys; for (int i = 0; i < keys.Length; i++) { if (keys[i].ToLower() != "page") url.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]); } StringBuilder sb = new StringBuilder(); sb.Append("<tr><td>"); sb.AppendFormat("總共{0}條記錄,共{1}頁,當前第{2}頁 ", recordCount, pageCount, currentPageIndex); if (currentPageIndex == 1) sb.Append("<span>首頁</span> "); else { string url1 = string.Format(url.ToString(), 1); sb.AppendFormat("<span><a href={0}>首頁</a></span> ", url1); } if (currentPageIndex > 1) { string url1 = string.Format(url.ToString(), currentPageIndex - 1); sb.AppendFormat("<span><a href={0}>上一頁</a></span> ", url1); } else sb.Append("<span>上一頁</span> "); if (mode == PageMode.Numeric) sb.Append(GetNumericPage(currentPageIndex, pageSize, recordCount, pageCount, url.ToString())); if (currentPageIndex < pageCount) { string url1 = string.Format(url.ToString(), currentPageIndex + 1); sb.AppendFormat("<span><a href={0}>下一頁</a></span> ", url1); } else sb.Append("<span>下一頁</span> "); if (currentPageIndex == pageCount) sb.Append("<span>末頁</span> "); else { string url1 = string.Format(url.ToString(), pageCount); sb.AppendFormat("<span><a href={0}>末頁</a></span> ", url1); } return sb.ToString(); } /// <summary> /// 獲取數字分頁 /// </summary> /// <param name="currentPageIndex"></param> /// <param name="pageSize"></param> /// <param name="recordCount"></param> /// <param name="pageCount"></param> /// <param name="url"></param> /// <returns></returns> private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount, string url) { int k = currentPageIndex / 10; int m = currentPageIndex % 10; StringBuilder sb = new StringBuilder(); if (currentPageIndex / 10 == pageCount / 10) { if (m == 0) { k--; m = 10; } else m = pageCount % 10; } else m = 10; for (int i = k * 10 + 1; i <= k * 10 + m; i++) { if (i == currentPageIndex) sb.AppendFormat("<span><font color=red><b>{0}</b></font></span> ", i); else { string url1 = string.Format(url.ToString(), i); sb.AppendFormat("<span><a href={0}>{1}</a></span> ", url1, i); } } return sb.ToString(); } } /// <summary> /// 分頁模式 /// </summary> public enum PageMode { /// <summary> /// 普通分頁模式 /// </summary> Normal, /// <summary> /// 普通分頁加數字分頁 /// </summary> Numeric } }
html代碼
<div id="pageNav" class="pageinator"> @Html.ShowPageNavigate((int)ViewData["pageindex"], (int)ViewBag.pageSize, (int)ViewBag.totalCount); </div>
控制器
shopEntities shop = new shopEntities(); public ActionResult Index() { //IQueryable<tbl_order> order = (from a in shop.tbl_order select a).OrderBy(a=>a.id).Skip(10).Take(10); //ViewData["order"] = order; //return View(); int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]); int pageSize = Request["pageSize"] == null ? 10 : int.Parse(Request["pageSize"]); int totalCount = 0; //給前臺傳遞 分頁的參數數據 ViewData["pageIndex"] = pageIndex; //ViewData["pageSize"] = pageSize; ViewBag.pageSize = pageSize; //總條數 totalCount = shop.tbl_order.Count(); ViewBag.totalCount = totalCount; //把當前頁面數據發送到前臺。 //ViewData.Model = db.UserInfo.ToList(); //List<tbl_order> pp = shop.tbl_order // .OrderBy(u => u.id) // .Skip((pageIndex - 1) * pageSize) // .Take(pageSize).ToList(); IQueryable<tbl_order> pp = shop.tbl_order .OrderBy(u => u.id) .Skip((pageIndex - 1) * pageSize) .Take(pageSize); return View(pp); }
關于“asp.net MVC怎樣分頁”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。