I've already implemented ASP. NET MVC paging (view that blog post), but it has limitations and must ensure that only one page can be found in the same view, and if you need to set up multiple paging in the same view, but I'm not able to do that, I re-optimized the original code. Added more flexible configuration properties and generation rules to solve the above problem, the code is as follows:
First, PageInfo class
Using system;using system.collections.generic;using system.linq;using system.web; namespace ROIS. models{ ///<summary> //pagination information //</summary> public class pageinfo { private int _recordcount = 0; Private int _ PageSize = 10; private int _currentpageno = 1; private string _pagen Octrlname = "_pageno"; private bool _createscript = true; //<SU mmary> //Get or set total records //</summary> &NB Sp public int recordcount { get &NBSP ; { return _recordcount; &NB Sp } &NBSP; set { if (VA Lue > 0) { &NBSP ; _recordcount = value; } &N Bsp } } //<summary> /// Gets or sets the number of records per page //</summary> public int pagesize &NB Sp { get { &NBSP ; return _pagesize; } set { if (value &G T 0) &nbsP { _p Agesize = value; } } } //<summary> ///Get or set the current index page number (from 1 Start calculation) //</summary> public int currentpageno &NB Sp { get { &NBSP ; return _currentpageno; } &N Bsp set { if (VA Lue > 0) { &NBSP ;   if (value > this. PageCount) { _currentpageno = this. pagecount; } else { &NBS P _currentpageno = value; & nbsp } } & nbsp } } //<summary> ///Get Total pages &n Bsp //</summary> public int pagecount {&nbs P &NBSP get { if (this. RecordCount <= 0) { &NB Sp return 1; } & nbsp return this. Recordcount/this. PageSize + (this. RecordCount% this. PageSize > 0? 1:0); } } // <summary> //Get or set the name of the control that holds the page number //</summary>   ; public string pagenoctrlname { get { return _pagenoctrlname; } set { &NBSP ; if (!string. IsNullOrEmpty (value)) { &N Bsp _pagenoctrlname = value; } } } //<summary> &NB Sp //Whether you need to create a script function //</summary> public bool createscript& nbsp { get {return _createscript;} set {_createscript = value;} } public PageInfo () {}   ; public PageInfo (int RecordcounT, int currentpageno, int pageSize = ten, string pagenoctrlname = NULL, bool Createscript = true) &NBSP ; { this. RecordCount = recordcount; this. PageSize = pagesize; this. Currentpageno = currentpageno; this. Pagenoctrlname = pagenoctrlname; this. Createscript = createscript; } //<SUMMARY>&NBSP ; //whether homepage //</summary> //<returns& gt;</returns> public bool Isfirstpage () { return (this. Currentpageno <= 1); } //<summary> &nbs P //Is last //</summary> //<returns></retur ns> public bool Islastpage () { Return (this. Currentpageno >= this. PageCount); } }}
Second, _pager partial view (recommended in shared directory)
@using ROIS. Models, @model pageinfo @if (model!=null && model.recordcount > 0) { &NBSP;&NBS P;<div class= "Pager" > @ (Model.currentpageno) page / total @ (@Model. PageCount) page, @if ( Model.isfirstpage ()) { <span>|< page </span> <span >< prev </span>}else{ <a href= "javascript:turnpage (1," @Model. Pagenoctrlname ");" >|< First page </a> <a href= "Javascript:turnpage (@ ( model.currentpageno-1), "@Model. Pagenoctrlname"); " >< prev </a>} @if (Model.islastpage ()) { <span> next ></span> <span> End page >|</span>}else{ <a href= "Javascript:turnpage (@ (model.currentpageno+1), "@Model. Pagenoctrlname"); " > Next ></a> <a href= "Javascript:turnpage (@Model. PageCount," @Model. pagenoctRlname ");" > End page >|</a> go to: <select id= "pages" onchange= "Javascript:turnpage (This.value, "@Model. Pagenoctrlname"); " > @for (int i = 1; I <= model.pagecount; i++) { if (Model.curre Ntpageno = = i) { <option value= "@i" selected= "selected" > @ (i) page </option> } else { &NB Sp <option value= "@i" > @ (i) page </option> } }</select>< Input type= "hidden" id= "@Model. Pagenoctrlname" Name= "@Model. Pagenoctrlname"/></div>if (model.createscript ) {<script type= "text/javascript" ><!-- function turnpage (pageno,ctrlid) { var Opageno = document.getElementById (ctrlid); Opageno.value = pageno; &nbs P OPageNo.form.submit (); } function getform (obj) { if (Obj.paren TNode.nodeName.toLowerCase () = = "Form") { return obj.parentnode; &NB Sp Else { GetForm (obj.parentnode); }   ; }//--></script>} }
Third, how to use: Background controller action to add:
String pageno = request.form["_pageno"]; int Ipageno = 1; Int. TryParse (PageNo, out Ipageno); PageInfo PageInfo = new PageInfo (Detaillist.count (), Ipageno, +, "_pageno", false); Viewbag.pageinfo = PageInfo;
PageNo = request.form["_pageno2"]; Ipageno = 1; Int. TryParse (PageNo, out Ipageno); PageInfo PageInfo2 = new PageInfo (Detaillist2.count (), Ipageno, +, "_pageno2", true);
Viewbag.pageinfo2 = PageInfo2;
The Front view page code is as follows: (Note: Rois is my project name, replace according to the actual situation)
@Html. Partial ("_pager", Viewbag.pageinfo as ROIS. Models.pageinfo) @Html. Partial ("_pager", Viewbag.pageinfo2 as ROIS. Models.pageinfo)
Article sync posted on my personal site: http://www.zuowenjun.cn/post/2014/10/23/65.html
An improved version of the ASP. NET MVC page implementation-Add the same view to set multiple pagination