ASP. net mvc paging implementation, asp. netmvc Paging
ASP.. net mvc does not support paging controls. Therefore, I wrote a partial page view by myself. In combination with the PageInfo class, pages can be displayed anywhere on any page, because the POST-based paging method is used, the only restriction is that it must be placed in FORM. Of course, I will consider implementing URL-based paging later!
1. PageInfo class
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 6 namespace ROIS. models 7 {8 /// <summary> 9 // page information 10 /// </summary> 11 public class PageInfo 12 {13 private int _ RecordCount = 0; 14 private int _ PageSize = 10; 15 private int _ CurrentPageNo = 1; 16 17 /// <summary> 18 // get or set the total number of records 19 /// </summary> 20 public int RecordCount 21 {22 get 23 {24 retur N _ RecordCount; 25} 26 set 27 {28 if (value> 0) 29 {30 _ RecordCount = value; 31} 32} 33} 34 35 // <summary> 36 // get or set the number of records per page 37 /// </summary> 38 public int PageSize 39 {40 get {41 return _ PageSize; 42} 43 set {44 if (value> 0) 45 {46 _ PageSize = value; 47} 48} 49} 50 51 // <summary> 52 // obtain or set the current index page number (from 1) 53 // </summary> 54 public int CurrentPageNo 55 {56 get {57 return _ CurrentPageNo; 58} 59 60 set {61 if (value> 0) 62 {63 if (value> this. pageCount) 64 {65 _ CurrentPageNo = this. pageCount; 66} 67 else 68 {69 _ CurrentPageNo = value; 70} 71} 72} 73} 74 75 // <summary> 76 // obtain the total number of pages 77 // </summary> 78 public int PageCount 79 {80 get 81 {82 if (this. recordCount <= 0) 83 {84 return 1; 85} 86 87 return this. recordCount/this. pageSize + (this. recordC Ount % this. PageSize> 0? 1: 0); 88} 89} 90 91 public PageInfo () 92 {} 93 94 public PageInfo (int recordCount, int currentPageNo, int pageSize = 10) 95 {96 this. recordCount = recordCount; 97 this. pageSize = pageSize; 98 this. currentPageNo = currentPageNo; 99} 100 101 102 // <summary> 103 // whether the homepage is 104 // </summary> 105 // <returns> </returns> 106 public bool isFirstPage () 107 {108 return (this. currentPageNo <= 1 ); 109} 110 111 112 // <summary> 113 // whether it is the last page 114 // </summary> 115 // <returns> </returns> 116 public bool isLastPage () 117 {118 return (this. currentPageNo> = this. pageCount); 119} 120 121} 122} 123
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);" >|& lt; first & nbsp; page </a> <a href = "javascript: turnPage (@ (Model. currentPageNo-1); "> & lt; previous </a >}@ if (Model. isLastPage () {<span> next page & gt; </span> <span> last & nbsp; Page & gt; | </ Span >}else {<a href = "javascript: turnPage (@ (Model. currentPageNo + 1); "> next page & gt; </a> <a href =" javascript: turnPage (@ Model. pageCount); "> end & nbsp; Page & gt; | </a >}go to: <select id =" pages "onchange =" javascript: turnPage (this. value); "> @ for (int I = 1; I <= Model. pageCount; I ++) {if (Model. currentPageNo = I) {<option value = "@ I" selected = "selected"> NO. @ (I) page </option>} else {<option value = "@ I"> page @ (I) </option>}} </Select> <input type = "hidden" id = "_ pageno" name = "_ pageno"/> </div> <script type = "text/javascript"> <! -- Function turnPage (pageNo) {var oPageNo = document. getElementById ("_ pageno"); oPageNo. value = pageNo; oPageNo. form. submit ();} function getForm (obj) {// This is not used, but it can replace the above oPageNo. form 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 (5000, iPageNo, 20 );
ViewBag. PageInfo = pageInfo;
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)
@ Using (Html. BeginForm ())
{
Here is the data list HTML code
@ Html. Partial ("_ Pager", ViewBag. PageInfo as ROIS. Models. PageInfo)
}
From my personal website: http://www.zuowenjun.cn/post/2014/08/26/24.html