asp.net MVC分页代码分享
程序员文章站
2023-10-23 22:32:11
本文实例为大家分享了mvc分页代码,供大家参考,具体内容如下
using system.collections.generic;
using system.co...
本文实例为大家分享了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类型转换类(通用类)代码分享
-
ASP.NET MVC4 HtmlHelper扩展类,实现分页功能
-
ASP.NET餐饮管理系统制作代码分享
-
ASP.NET MVC 2右键菜单和简单分页实例讲解
-
使用ASP.NET中关于代码分离的实例分享
-
ASP.NET MVC 5使用X.PagedList.Mvc进行分页教程(PagedList.Mvc)
-
Asp.Net类型转换类(通用类)代码分享
-
ASP.NET MVC4 HtmlHelper扩展类,实现分页功能
-
使用ASP.NET中关于代码分离的实例分享
-
ASP.NET MVC 5使用X.PagedList.Mvc进行分页教程(PagedList.Mvc)