Get the App
SLTechnology News&Howtos  ›  Development  › 

How to paginate asp.net MVC

Shulou Source: shulou.com Published: 2022-06-03 13:01:49 10月01日 Update

This article will explain in detail how to divide pages on asp.net MVC. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

The details are as follows

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 {/ paging / paging id / / current page / paging size / Total number of records / paging header tag attributes / paging style / / paging mode / 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 () } / paging / paging id / / current page / / paging size / Total number of records / paging style / 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) } / paging / paging id / / current page / / paging size / Total number of records / public static string Pager (this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount) {return Pager (helper, id, currentPageIndex, pageSize, recordCount, null) } / paging / paging id / / current page / / paging size / / Total number of records / paging mode / 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) } / paging / paging id / / current page / paging size / Total number of records / paging style / paging mode / 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) } / get normal paging / 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(""); sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页 ", recordCount, pageCount, currentPageIndex); if (currentPageIndex == 1) sb.Append("首页 "); else { string url1 = string.Format(url.ToString(), 1); sb.AppendFormat("首页 ", url1); } if (currentPageIndex >

1) {string url1 = string.Format (url.ToString (), currentPageIndex-1); sb.AppendFormat (previous page, url1);} else sb.Append (previous page); 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("下一页 ", url1); } else sb.Append("下一页 "); if (currentPageIndex == pageCount) sb.Append("末页 "); else { string url1 = string.Format(url.ToString(), pageCount); sb.AppendFormat("末页 ", url1); } return sb.ToString(); } /// /// 获取数字分页 /// /// /// /// /// /// /// 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 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 pp = shop.tbl_order // .OrderBy(u =>

U.id) / / .Skip ((pageIndex-1) * pageSize) / / .Take (pageSize) .ToList (); IQueryable pp = shop.tbl_order .OrderBy (u = > u.id) .Skip ((pageIndex-1) * pageSize) .Take (pageSize); return View (pp) } this is the end of the article on "how to Page asp.net MVC". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Dimensions totals patterns styles articles content foreground numbers data more previous home good practical code parameters properties controllers articles tags Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi OPPO Reno Shulou Information Huawei