In MVC, the extension method can be used to add HTML helper methods to implement paging, so that the page can be easily reused code, the code is as follows
1 Public StaticMvchtmlstring Pagelinks ( ThisHtmlHelper HTML,intCurpage,intTotalpage, func<int,string>pageurl)2 {3StringBuilder result =NewStringBuilder ();4 intStart =1, end =Totalpage;5Start = Curpage-5<1?1: Curpage-5;6End = Curpage +5> totalpage? Totalpage:curpage +5;7 8 if(Curpage >1)9 {TenTagbuilder prev =NewTagbuilder ("a"); OnePrev. Mergeattribute ("href", Pageurl (Curpage-1)); APrev. InnerHtml ="<"; - result. Append (prev. ToString ()); - } the - - for(inti = start; I <= end; i++) - { +Tagbuilder tag =NewTagbuilder ("a"); -Tag. InnerHtml =i.tostring (); + if(I! =curpage) A { atTag. Mergeattribute ("href", Pageurl (i)); - } - Else - { -Tag. Mergeattribute ("href","javascript:;"); -Tag. addCssClass ("Active"); in } - result. Append (tag. ToString ()); to } + - if(Curpage <totalpage) the { *Tagbuilder next =NewTagbuilder ("a"); $Next. Mergeattribute ("href", Pageurl (Curpage-1));Panax NotoginsengNext. InnerHtml =">"; - result. Append (next. ToString ()); the } + A the returnmvchtmlstring.create (result. ToString ()); +}
After you add the above code, you need a simple line of code to create a paging link in the view, as shown below, page is the current page, Totalpage is the total number of pages, and the third parameter is a generated code link
@Html. Pagelinks (page, totalpage, x = Url.action ("Action", new {page=x;}))
ASP. NET MVC uses HTML helper for paging