Previous cool MVC: Creates perfect pages step by step. Some friends don't know how to use it. I thought I wrote too much details. After all, I posted too many posts on my blog.CodeUnsightly. This is a complete example, and the code is attached for download by people who need it. It is estimated that no one will ask how to use it.
In fact, the previous Code has two bugs,
1. generally, the pagination URL is in the following format:/home/index/page/1. The first page does not display the page number, that is, when no page number is specified, the URL is: /home/index. At this time, if you press the next page, the original code will report an error saying that the "page" keyword cannot be found.
2. Missing query characters. If the URL is/home/index? Page = 1 & User = Bruce, the next page will become like this:/home/index? Page = 2, user = Bruce is lost
The corrected code is attached here:
Public Static Class Pagerhelper { /// <Summary> /// Pager display /// </Summary> /// <Param name = "html"> </param> /// <Param name = "currentpagestr"> querystringkey that identifies the current page number </param> /// <Param name = "pagesize"> display on each page </param> /// <Param name = "totalcount"> total data volume </param> /// <Returns> </returns> Public Static String Pager ( This Htmlhelper HTML, String Currentpagestr, Int Pagesize, Int Totalcount) {var querystring = html. viewcontext. httpcontext. Request. querystring; Int Currentpage = 1; // Current page VaR totalpages = math. Max (totalcount + pagesize-1)/pagesize, 1 ); // Total number of pages VaR dict = New System. Web. Routing. routevaluedictionary (html. viewcontext. routedata. values); var output = New Stringbuilder (); If (! String . Isnullorempty (querystring [currentpagestr]) { // Bind to the corresponding querystring Foreach ( String Key In Querystring. Keys)If (Querystring [Key]! = Null &&! String . Isnullorempty (key) dict [Key] = querystring [Key]; Int . Tryparse (querystring [currentpagestr], Out Currentpage );} Else { // Obtain ~ /Page/{page number} page number Parameter If (Dict. containskey (currentpagestr )) Int . Tryparse (dict [currentpagestr]. tostring (), Out Currentpage );}// Retain query characters to the next page Foreach ( String Key In Querystring. Keys) dict [Key] = querystring [Key]; // If necessary, keep the form value to the next page (I do not need it now, so comment it out) // Var formvalue = html. viewcontext. httpcontext. Request. form; // Foreach (string key in formvalue. Keys) // If (formvalue [Key]! = NULL &&! String. isnullorempty (key )) // Dict [Key] = formvalue [Key]; If (Currentpage <= 0) currentpage = 1; If (Totalpages> 1 ){If (Currentpage! = 1 ){ // Process the homepage connection Dict [currentpagestr] = 1; output. appendformat ( "{0 }" , HTML. routelink ( "Homepage" , Dict ));} If (Currentpage> 1 ){ // Process the connection of the previous page Dict [currentpagestr] = currentpage-1; output. append (html. routelink ( "Previous Page" , Dict ));} Else {Output. append ( "Previous Page" );} Output. append ( "" ); Int Currint = 5; For ( Int I = 0; I <= 10; I ++ ){ // A total of 10 page numbers can be displayed, including the first 5 and the last 5 If (Currentpage + I-currint)> = 1 & (currentpage + I-currint) <= totalpages) If (Currint = I ){ // Process the current page Output. append ( String . Format ( "[{0}]" , Currentpage ));} Else { // General page processing Dict [currentpagestr] = currentpage + I-currint; output. append (html. routelink (currentpage + I-currint). tostring (), dict);} output. append ( "" );} If (Currentpage <totalpages ){ // Process the next page Dict [currentpagestr] = currentpage + 1; output. append (html. routelink ( "Next page" , Dict ));}Else {Output. append ( "Next page" );} Output. append ( "" ); If (Currentpage! = Totalpages) {dict [currentpagestr] = totalpages; output. append (html. routelink ( "Last page" , Dict);} output. append ( "" );} Output. appendformat ( "{0}/{1 }" , Currentpage, totalpages ); // This statistics can be added without adding Return Output. tostring ();}}
The following is a complete example: mvcpaging.rar.