Three important components are posted here.
1. Main algorithm code in the paging navigation bar (including the @ Html Extension Method)
2. HTML page Call Code (including navigation bar style)
3. Controller code
4. Testing path: http://www.iqingcao.com/EBusiness/Paination/2? Pc = 10 (here is only a local test url instance, different sub-test Domain Names (www.iqingcao.com) need to be replaced)
Parameter description: pc (pageCount), total number of pages
2 of them are: pageIndex, the index of the current page; the default route is used, that is, the routing definition in the form of {controller}/{action}/{id }.
5. Display Results
Note: The implementation is simple. If you have time to enrich the configuration parameters, you can make them more configurable.
I also use the rich post.
Bytes ------------------------------------------------------------------------------------------------------------------------------
1. Main algorithm code in the paging navigation bar
Using System;
Using System. Text;
Using System. Web. Mvc;
Namespace EB. Ctrl. Utility
{
# Region Mvc pagination bar Extension Method
/// <Summary>
/// Mvc pagination bar Extension Method
/// </Summary>
Public static class HtmlPaginationBar
{
/// <Summary>
/// Generate a paging bar (page call @ Html. PaginationBar)
/// </Summary>
/// <Param name = "helper"> </param>
/// <Param name = "bar"> pagination bar generator </param>
/// <Returns> </returns>
Public static MvcHtmlString PaginationBar (this HtmlHelper helper, PaginationBarBilder bar)
{
Return new MvcHtmlString (bar. GenPaginationHtml ());
}
}
# Endregion
# Region page entry parameters
/// <Summary>
/// Page entry parameters
/// </Summary>
Public class PageBarPars
{
/// <Summary>
/// Total number of pages
/// </Summary>
Public int PageCount {set; get ;}
/// <Summary>
/// Index of the current page
/// </Summary>
Public int PageIndex {set; get ;}
}
# Endregion
# Region paging navigation bar Generator
Public class PaginationBarBilder
{
/// <Summary>
/// Paging navigation bar Generator
/// </Summary>
/// <Param name = "url"> page address template. Replace {0} with the changed page index. </param>
/// <Param name = "pageCount"> total number of pages </param>
/// <Param name = "pageIndex"> current page index </param>
Public PaginationBarBilder (string url, int pageCount, int pageIndex)
{
M_url = url;
M_pageCount = pageCount;
M_pageIndex = pageIndex;
}
Private int m_pageIndex = 0;
Private int m_pageCount = 0;
Private string m_url = string. Empty;
Private int m_offset = 3;
Private StringBuilder m_html = new StringBuilder ();
/// <Summary>
/// Generate the page m_html code
/// </Summary>
/// <Returns> </returns>
Public string GenPaginationHtml ()
{
StringBuilder m_html = new StringBuilder ();
M_html.Append ("<div id = 'pagination'> ");
M_html.Append (GenPrevious ());
M_html.Append (GenLeftAnchor ());
M_html.Append (GenMidArea ());
M_html.Append (GenRightAnchor ());
M_html.Append (GenNext ());
M_html.Append ("</div> ");
Return m_html.ToString ();
}
/// <Summary>
/// Generate the previous page
/// </Summary>
Private string GenPrevious ()
{
StringBuilder m_html = new StringBuilder ();
String href = string. Empty;
If (m_pageIndex <= 0 | m_pageCount <= 1)
{
M_html.Append ("<span class = 'current prev'> previous page </span> ");
Return m_html.ToString ();
}
Href = string. Format (m_url, m_pageIndex-1 );
M_html.Append (
String. Format ("<a class = 'prev' href = '{0}'> previous page </a>", href ));
Return m_html.ToString ();
}
/// <Summary>
/// Generate the next page
/// </Summary>
Private string GenNext ()
{
StringBuilder m_html = new StringBuilder ();
String href = string. Empty;
If (m_pageIndex> = m_pageCount-1)
{
M_html.Append ("<span class = 'current next'> next page </span> ");
Return m_html.ToString ();
}
Href = string. Format (m_url, ++ m_pageIndex );
M_html.Append (
String. Format ("<a class = 'prev' href = '{0}'> next page </a>", href ));
Return m_html.ToString ();
}
/// <Summary>
/// Left-Stop connection
/// </Summary>
Private string GenLeftAnchor ()
{
StringBuilder m_html = new StringBuilder ();
String href = string. Empty;
If (m_pageIndex-m_offset> 0)
{
Href = string. Format (m_url, 0 );
M_html.Append (string. Format ("<a href = '{0}'> 1 </a>", href ));
}
If (m_pageIndex-m_offset> 1)
M_html.Append (string. Format ("<span>... </span> "));
Return m_html.ToString ();
}
/// <Summary>
/// Right-Stop connection
/// </Summary>
Private string GenRightAnchor ()
{
If (m_pageIndex + m_offset> = m_pageCount-1)
Return string. Empty;
StringBuilder m_html = new StringBuilder ();
String href = string. Empty;
If (m_pageIndex + m_offset <m_pageCount-2)
M_html.Append (string. Format ("<span>... </span> "));
If (m_pageIndex + m_offset <m_pageCount)
{
Href = string. Format (m_url, m_pageCount-1 );
M_html.Append (string. Format ("<a href = '{0}'> {1} </a>", href, m_pageCount ));
}
Return m_html.ToString ();
}
/// <Summary>
/// Generate the intermediate paging button Section
/// </Summary>
Private string GenMidArea ()
{
StringBuilder m_html = new StringBuilder ();
String href = string. Empty;
If (m_pageCount = 1)
{
M_html.Append ("<span class = 'current'> 1 </span> ");
Return m_html.ToString ();
}
Action <int> addHtml = (index =>
{
If (index = m_pageIndex)
{
Href = string. Format ("<span class = 'current'> {0} </span>", index + 1 );
M_html.Append (href );
Return;
}
Href = string. Format (m_url, index );
Href = string. Format ("<a href = '{0}'> {1} </a>", href, index + 1 );
M_html.Append (href );
});
Int start = 0;
Int end = 0;
If (m_pageIndex <m_offset)
{
Start = 0;
If (m_offset + m_offset <m_pageCount)
End = m_offset + m_offset;
Else
End = m_pageCount-1;
}
Else
{
Start = m_pageIndex-m_offset;
If (m_pageIndex + m_offset> = m_pageCount)
End = m_pageCount-1;
Else
End = m_pageIndex + m_offset;
}
For (int I = 0; I <m_pageCount; I ++)
{
If (I <start | I> end)
Continue;
AddHtml (I );
}
Return m_html.ToString ();
}
}
# Endregion
}
Bytes ------------------------------------------------------------------------------------------------------------------------------
2. HTML page Call Code (including navigation bar style)
@{
Layout = null;
}
@ Using EB. Ctrl. Utility;
<! DOCTYPE html>
<Html>
<Head>
<Title> Paination </title>
<Style>
# Pagination {float: right}
# Pagination {
Text-decoration: none;
Border: 1px solid # AAE;
Color: #666;
}
# Pagination a: hover {
BORDER-BOTTOM: # c00 1px solid; BORDER-LEFT: # c00 1px solid; BACKGROUND: # c00; COLOR: # fff; BORDER-TOP: # c00 1px solid; BORDER-RIGHT: # c00 1px solid; TEXT-DECORATION: none
}
# Pagination a, # pagination span {
Display: inline-block;
Padding: 0.1em 0.4em;
Margin-right: 5px;
Margin-bottom: 5px;
}
# Pagination. current {
Background: # bc0057;
Color: # fff;
Border: 1px solid # c00;
}
# Pagination. current. prev, # pagination. current. next {
Color: #999;
Border-color: #999;
Background: # fff;
}
</Style>
</Head>
<Body>
<! -- To use this extension page, you must reference the using EB. Ctrl. Utility namespace. -->
@ Html. PaginationBar (new PaginationBarBilder ("http://www.iqingcao.com/ebusiness/paination/00000 }? Pc = "+ this. ViewBag. PagePars. PageCount. ToString (), this. ViewBag. PagePars. PageCount, this. ViewBag. PagePars. PageIndex ))
</Body>
</Html>
Bytes ------------------------------------------------------------------------------------------------------------------------------
3. Controller code
Using System. Web. Mvc;
Using EB. Ctrl. Utility;
Using EB. Sys. Extension;
Namespace EB. Ctrl. Controllers
{
Public class EBusinessController: Controller
{
Public ActionResult Paination (string id, string pc)
{
PageBarPars pars = new PageBarPars ();
Pars. PageCount = pc. GetInt ();
Pars. PageIndex = id. GetInt ();
This. ViewBag. PagePars = pars;
Return View ();
}
}
}