One page of MVC, Which is excerpted from the Internet and slightly modified by yourself

Source: Internet
Author: User
Paging type

Using system. text;
Using system. Web. Routing;

Namespace system. Web. MVC
{
Public static class pagerhelper
{
/// <Summary>
/// Retrieve the page
/// </Summary>
/// <Param name = "urlformat"> URL format, with {0} Instead of page number </param>
/// <Param name = "pageindex"> current page </param>
/// <Param name = "pagesize"> total number of pages </param>
/// <Param name = "recordcount"> total number of records </param>
/// <Param name = "Mode"> paging mode </param>
/// <Returns> HTML </returns>
Public static string Pager (this htmlhelper helper, string urlformat, int pageindex, int pagesize, int recordcount, pagemode Mode)
{
Urlformat = urlformat. Replace ("% 7b0% 7D", "{0 }");
Int pagecount = (recordcount % pagesize = 0? Recordcount/pagesize: recordcount/pagesize + 1 );
Stringbuilder sb = new stringbuilder ();
SB. appendformat ("Total {0} records, total {1} pages, current page {2} & nbsp;", recordcount, pagecount, pageindex );
If (pageindex = 1)
SB. append ("homepage & nbsp ;");
Else
{
String url1 = string. Format (urlformat. tostring (), 1 );
SB. appendformat ("<a href = {0}> homepage </a> & nbsp;", url1 );
}
If (pageindex> 1)
{
String url1 = string. Format (urlformat. tostring (), pageindex-1 );
SB. appendformat ("<a href = {0}> previous page </a> & nbsp;", url1 );
}
Else
SB. append ("Previous Page & nbsp ;");
If (mode = pagemode. Numeric)
SB. append (getnumericpage (urlformat. tostring (), pageindex, pagesize, recordcount, pagecount ));
If (pageindex <pagecount)
{
String url1 = string. Format (urlformat. tostring (), pageindex + 1 );
SB. appendformat ("<a href = {0}> next page </a> & nbsp;", url1 );
}
Else
SB. append ("next page & nbsp ;");

If (pageindex = pagecount)
SB. append ("last page ");
Else
{
String url1 = string. Format (urlformat. tostring (), pagecount );
SB. appendformat ("<a href = {0}> last page </a>", url1 );
}
Return sb. tostring ();
}

/// <Summary>
/// Retrieve the number pagination
/// </Summary>
Private Static string getnumericpage (string URL, int pageindex, int pagesize, int recordcount, int pagecount)
{
Int K = pageindex/10;
Int M = pageindex % 10;
Stringbuilder sb = new stringbuilder ();
If (pageindex/10 = pagecount/10)
{
If (M = 0)
{
K --;
M = 10;
}
Else
M = pagecount % 10;
}
Else
M = 10;
For (INT I = K * 10 + 1; I <= K * 10 + m; I ++)
{
If (I = pageindex)
SB. appendformat ("<span style = \" color: Red \ ">{0} </span> & nbsp;", I );
Else
{
String url1 = string. Format (URL. tostring (), I );
SB. appendformat ("<a href = {0} >{1} </a> & nbsp;", url1, I );
}
}

Return sb. tostring ();
}
}

/// <Summary>
/// Paging Mode
/// </Summary>
Public Enum pagemode
{
/// <Summary>
/// Normal paging Mode
/// </Summary>
Normal,
/// <Summary>
/// Add numeric pagination to normal pagination
/// </Summary>
Numeric
}
}

 

<% = Html. Pager (URL. Action ("Main", new {page = "{0}"}), 1000, pagemode. Numeric) %>

 

Replace the page index parameter with {0}, which is very convenient to use.

Related Article

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.