十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章将为大家详细讲解有关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 { ////// 分页 /// /// /// 分页id /// 当前页 /// 分页尺寸 /// 记录总数 /// 分页头标签属性 /// 分页样式 /// 分页模式 ///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(); } /// /// 分页 /// /// /// 分页id /// 当前页 /// 分页尺寸 /// 记录总数 /// 分页样式 ///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); } /// /// 分页 /// /// /// 分页id /// 当前页 /// 分页尺寸 /// 记录总数 ///public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount) { return Pager(helper, id, currentPageIndex, pageSize, recordCount, null); } /// /// 分页 /// /// /// 分页id /// 当前页 /// 分页尺寸 /// 记录总数 /// 分页模式 ///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); } /// /// 分页 /// /// /// 分页id /// 当前页 /// 分页尺寸 /// 记录总数 /// 分页样式 /// 分页模式 ///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); } /// /// 获取普通分页 /// /// /// /// ///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("上一页 ", url1); } else sb.Append("上一页 "); 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 <= k * 10 + m; i++) { if (i == currentPageIndex) sb.AppendFormat("{0} ", i); else { string url1 = string.Format(url.ToString(), i); sb.AppendFormat("{1} ", url1, i); } } return sb.ToString(); } } /// /// 分页模式 /// public enum PageMode { ////// 普通分页模式 /// Normal, ////// 普通分页加数字分页 /// Numeric } }html代码
@Html.ShowPageNavigate((int)ViewData["pageindex"], (int)ViewBag.pageSize, (int)ViewBag.totalCount);控制器
shopEntities shop = new shopEntities(); public ActionResult Index() { //IQueryableorder = (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 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); } 关于“asp.net MVC怎样分页”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
网页标题:asp.netMVC怎样分页-创新互联
本文地址:http://6mz.cn/article/dhcpds.html