ASP. NET MVC uses HTML helper for paging

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.