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
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.
Pagination when querying with aspnet mvc
You do not need to pass the condition from the post action to the get action. Although yes, this is not recommended.
A better way is to use filter attribute. The form of your post action may be as follows:
[HttpPost] // you may not have used it like this, or you may have used it like this ..
Public ViewResult MyAction (int page)
{//...}
So you should know (or can) to do this:
[HttpGet]
Public ViewResult MyAction (int page)
{//...}
The form parameter page of MyAction is provided by the "value provider" of ASP. net mvc. It binds a value to the page from the list by priority:
Request. Form
RouteData. Values
Request. QueryString
Request. Files
This means that your condition can start with a <input name = "page" type = "... "> the html field receives user input. When you submit a form, the field name is equal to the field in the form of page (name =" page ") (that is, html input) the value of is used as the parameter value of MyAction, and the value of form field is the first.
For additional questions:
Understand what you mean. You want to Get the request without exposing the condition to the URL. If so, there is a good way to directly use Pro ASP. NET MVC2 Framework (second edition) Chapter 4 is the idea of paging example in chapter 5.
The general idea is to make each page correspond to a URL. One of the advantages is that when the customer looks at the content of a page (assuming it is the third page, right-click the page and select "add to Favorites". When he opens the favorites link from his favorites several days later, he will still get the data on the third page. This is difficult to achieve through a simple method in Web Form.
To achieve this, you need to maintain a View Model with the CurrentPageIndex attribute and add a CurrentPageIndex item When configuring the Route ..
The dormitory will be powered off. If you don't want to download the ebook (of course, I recommend you), continue tomorrow ..
I hope this will be a supplement to the final version:
Below is Steven Sanderson's Pro ASP. NET MVC2 Framework (second edition), and some of my instructions (the following are based on your understanding of C # or new or old syntax ):
The overall idea is to turn "Paging" into a component, facilitating reuse.
Specifically, first extend a method to HtmlHelper (related documentation on Extensible Method parameters, such as MSDN). This method returns an MvcHtmlString object that can be parsed by the <%> syntax, in fact, the content contained in this object is an HTML tag <a>. The extension method is as follows:
Public static class PagingHelpers
{
Public static MvcHtmlString PageLinks (this HtmlHelper html, PagingInfo pageInformation,
Func <int, string> pageUrl)
{
StringBuilder result = new StringBuilder () ...... the remaining full text>