ASP. net mvc paging implementation-added multiple tabs for the same view, asp. netmvc
I have implemented ASP before. net mvc paging (view this blog post), but it has limitations. You must ensure that only one page is allowed in the same view. If you need to set multiple pages in the same view, I have optimized the original code, added more flexible attribute configuration and generation rules, and solved the above problem. The Code is as follows:
1. PageInfo class
Using System; using System. collections. generic; using System. linq; using System. web; namespace ROIS. models {/// <summary> /// page information /// </summary> public class PageInfo {private int _ RecordCount = 0; private int _ PageSize = 10; private int _ CurrentPageNo = 1; private string _ PageNoCtrlName = "_ pageno"; private bool _ CreateScript = true; /// <summary> /// obtain or set the total number of records /// </summary> public int RecordCount {Get {return _ RecordCount;} set {if (value> 0) {_ RecordCount = value ;}}} /// <summary> /// get or set the number of records per page /// </summary> public int PageSize {get {return _ PageSize ;} set {if (value> 0) {_ PageSize = value ;}}/// <summary> // obtain or set the current index page number (calculated from 1) /// </summary> public int CurrentPageNo {get {return _ CurrentPageNo;} set {if (value> 0) {if (value> this. pageCount) {_ CurrentPageN O = this. pageCount;} else {_ CurrentPageNo = value ;}}}} /// <summary> /// obtain the total number of pages /// </summary> public int PageCount {get {if (this. recordCount <= 0) {return 1;} return this. recordCount/this. pageSize + (this. recordCount % this. pageSize> 0? 1: 0 );}} /// <summary> /// obtain or set the control name for storing the page number. // </summary> public string PageNoCtrlName {get {return _ PageNoCtrlName;} set {if (! String. isNullOrEmpty (value) {_ PageNoCtrlName = value ;}}} /// <summary> /// whether to create a script function // </summary> public bool CreateScript {get {return _ CreateScript ;} set {_ CreateScript = value ;}} public PageInfo () {}public PageInfo (int recordCount, int currentPageNo, int pageSize = 10, string pageNoCtrlName = null, bool createScript = true) {this. recordCount = recordCount; this. pageSize = pageSize; this. currentPageNo = currentPageNo; this. pageNoCtrlName = pageNoCtrlName; this. createScript = createScript;} // <summary> // whether it is the Home Page // </summary> /// <returns> </returns> public bool IsFirstPage () {return (this. currentPageNo <= 1 );} /// <summary> /// whether it is the last page // </summary> /// <returns> </returns> public bool IsLastPage () {return (this. currentPageNo> = this. pageCount );}}}
Ii. _ Pager local view (it is recommended to put it under the Shared directory)
@ Using ROIS. Models; @ model PageInfo @ if (Model! = Null & Model. recordCount> 0) {<div class = "pager"> @ (Model. currentPageNo) Page & nbsp;/& nbsp; @ (@ Model. pageCount page, @ if (Model. isFirstPage () {<span >|& lt; Header & nbsp; page </span> <span> & lt; previous Page </span>} else {<a href = "javascript: turnPage (1," @ Model. pageNoCtrlName ");" >|& lt; Header & nbsp; page </a> <a href = "javascript: turnPage (@ (Model. currentPageNo-1), "@ Model. pageNoCtrlName ");"> & lt; previous page </a >}@ if (Model. isLastPage ()){ <Span> next page & gt; </span> <span> last & nbsp; Page & gt; | </span>} else {<a href = "javascript: turnPage (@ (Model. currentPageNo + 1), "@ Model. pageNoCtrlName ");"> next page & gt; </a> <a href = "javascript: turnPage (@ Model. pageCount, "@ Model. pageNoCtrlName ");"> last & nbsp; Page & gt; | </a >}go to: <select id = "pages" onchange = "javascript: turnPage (this. value, "@ Model. pageNoCtrlName ");"> @ for (int I = 1; I <= Model. pageCount; I ++) {if (Model. cur Repeated pageno = I) {<option value = "@ I" selected = "selected"> NO. @ (I) page </option>} else {<option value = "@ I"> NO. @ (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; oPageNo. form. submit ();} function getForm (obj) {if (obj. parentNode. nodeName. toLowerCase () = "form") {return obj. parentNode;} else {getForm (obj. parentNode) ;}}// --> </script> }}
Iii. Usage: add the following to the Action of the background Controller:
String pageNo = Request. form ["_ pageno"]; int iPageNo = 1; int. tryParse (pageNo, out iPageNo); PageInfo pageInfo = new PageInfo (detailList. count (), iPageNo, 30, "_ pageno", false); ViewBag. pageInfo = pageInfo;
PageNo = Request. form ["_ pageno2"]; iPageNo = 1; int. tryParse (pageNo, out iPageNo); PageInfo pageInfo2 = new PageInfo (detailList2.Count (), iPageNo, 30, "_ pageno2", true );
ViewBag. PageInfo2 = pageInfo2;
The code of the front-end VIEW page is as follows: (Note: ROIS is the name of my project, which should be changed according to the actual situation)
@ Html. Partial ("_ Pager", ViewBag. PageInfo as ROIS. Models. PageInfo) @ Html. Partial ("_ Pager", ViewBag. PageInfo2 as ROIS. Models. PageInfo)
Article published on my personal website: http://www.zuowenjun.cn/post/2014/10/23/65.html
Aspnet MVC3 achieves paging Effect
Paging, in fact, MVC is tied to link to call the background action. Therefore, you only need to add a page with the parameter on the page button, and query the page with the parameter when you go to the action, put it back into a view.
I cannot describe it clearly. You can check the buttons on Baidu pages. In fact, there is a parameter in the link that indicates the page number, and you will know how to paging.
ASPNET MVC2 is used as an example for simple addition, deletion, modification, and query. Generally, the query page is also used, but the query page cannot be supplemented.
High score ...... where is the score?
The key point is to submit the query parameters together with the page number when submitting the page... try to hide the domain. Use ViewModel.