Paging is complicated when I first wrote it myself. Most pages on the network are relatively mature paging space.CodeSometimes I really don't understand it. Recently I used mvc3 to view data by page.
1. Basic paging parameters: pagesize, pageindex, and total
Total, which is the total number of data to be queried. It is used to generate a paging control.
2. controll
Public actionresult paging (Int? Pageindex, int pagesize = 5)
{
Int Total = 0;
Pageindex = NULL? 0: pageindex;
// Obtain data
Mercurydbexentities DB = new mercurydbexentities ();
Total = dB. Users. Count ();
VaR result = dB. Users. orderby (P => P. ID). Skip (pageindex. Value * pagesize). Take (pagesize );
Viewbag. pagination = new pagination (pageindex, pagesize, total );
Return view (result );
}
3,
public class pagination
{< br> Public Int? Pageindex {Get; private set;} // the value of the current page number minus one
Public int pagesize {Get; private set ;} // Number of contents displayed on each page
Public int totalpages {Get; private set ;}// total number of pages
Public int start {Get; private set ;} // The first page number displayed on the current page (for example, in the middle of the page, the page number is between 9 and 16, and 9 is start)
Public int end {Get; private set;} // The last page number displayed on the current page
Public pagination (Int? Pageindex, int pagesize, int totalcount)
{
Pageindex = (pageindex ?? 0 );
Pagesize = pagesize ;;
Totalpages = (INT) math. Ceiling (totalcount/(double) pagesize );
Int size; // determines the number of page numbers displayed on each page
If (totalpages> 6) // This specifies that each page displays 6 page numbers.
{
Size = 6;
// Define the page number of each page.
If (pageindex> 2 & pageindex <totalpages-(size-2 ))
{
Start = (pageindex ?? 0)-1;
}
Else if (pageindex> = totalpages-(size-2 ))
{
Start = totalpages-size + 1;
}
Else
{
Start = 1;
}
}
Else
{
Size = totalpages;
Start = 1;
}
End = start + size-1;
}
// Used for the "Previous Page", "Next page" navigation
Public bool haspreviouspage
{
Get {return (pageindex> 0 );}
}
Public bool hasnextpage
{
Get {return (pageindex + 1 <totalpages );}
}
}
4. View
-- Content display
@ Model ienumerable <mvc3_test.database.users>
@ Using mvc3_test.htmlhelpers
@{
Viewbag. Title = "Paging ";
}
<H2> content display </H2>
@ Foreach (VAR item in Model)
{
<P>
@ Item. ID <B >|</B> @ item. loginname
</P>
}
<HR/>
@ Html. Partial ("_ pagination", (mvc3_test.htmlhelpers.pagination) viewbag. pagination)
-- Pagination Label display
@ Model mvc3_test.htmlhelpers.pagination
@ Using mvc3_test.htmlhelpers
<Div class = "Clearfix pagination">
<Div class = "pagination-inner">
@ If (model. Start> 1)
{
// If the current page exceeds the second page, the "1..." link of the first page is displayed.
String url = html. urlset (request, "pageindex", 0 );
<A Title = "first page 1..." href = "@ URL"> 1... </a>
}
@ For (INT I = model. Start; I <= model. end; I ++)
{
String url = html. urlset (request, "pageindex", I-1 );
If (I = model. pageindex + 1)
{
// Search for the current page number from several page numbers on the current page
<A Title = "Current page @ I. tostring ()" class = "current" href = "@ URL"> @ I. tostring () </a>
}
Else
{
<A Title = "go to the @ I. tostring () page @ I. tostring ()" href = "@ URL"> @ I. tostring () </a>
}
}
@ If (model. End <model. totalpages & model. pageindex-model. Start <2)
{
// Last page
String totalpages = (@ model. totalpages). tostring ();
String url = html. urlset (request, "pageindex", model. totalpages-1 );
<A Title = "last page @ totalpages" href = "@ URL"> @ totalpages </a>
}
</Div>
</Div>
[Note: As you can see, @ URL Processing is performed on the code in href of tag a, because sometimes there are many places in the project for displaying data, in order to make the "Page tag display page" reusable, another class is added: htmlhelpers]
5. htmlhelpers class
Public static class htmlhelpers
{
Public static string urlset (this htmlhelper helper, httprequestbase request, string queryname, object queryvalue)
{
If (queryname! = NULL & queryvalue! = NULL)
{
String Path = request. path;
Namevaluecollection query = new namevaluecollection (request. querystring );
Query. Set (queryname, queryvalue. tostring ());
Return createurl (path, query );
}
Return request. rawurl;
}
/// <Summary>
///
/// </Summary>
/// <Param name = "helper"> </param>
/// <Param name = "request"> </param>
/// <Param name = "queryname"> </param>
/// <Param name = "queryvalue"> </param>
/// <Returns> </returns>
Public static string urladd (this htmlhelper helper, httprequestbase request, string queryname, object queryvalue)
{
If (queryname! = NULL & queryvalue! = NULL)
{
String Path = request. path;
Namevaluecollection query = new namevaluecollection (request. querystring );
Query. Add (queryname, queryvalue. tostring ());
Return createurl (path, query );
}
Return request. rawurl;
}
Public static string urlremove (this htmlhelper helper, httprequestbase request, string queryname)
{
If (queryname! = NULL)
{
String Path = request. path;
Namevaluecollection query = new namevaluecollection (request. querystring );
Query. Remove (queryname );
Return createurl (path, query );
}
Return request. rawurl;
}
Public static string urlremove (this htmlhelper helper, httprequestbase request, string queryname, object queryvalue)
{< br> If (queryname! = NULL & queryvalue! = NULL)
{< br> string Path = request. Path;
namevaluecollection query = new namevaluecollection (request. querystring);
string [] values = query. getvalues (queryname);
List Vs = values. tolist ();
. remove (queryvalue. tostring ();
query. remove (queryname);
foreach (VAR item in VS)
{< br> query. add (queryname, item);
}
Return createurl (path, query );
}
Return request. rawurl;
}
Private Static string createurl (string path, namevaluecollection query)
{
String url = path;
If (query! = NULL & query. Count! = 0)
{
URL + = "? ";
For (INT I = 0; I <query. Count; I ++)
{
String key = query. getkey (I );
String [] values = query. getvalues (key );
For (Int J = 0; j <values. length; j ++)
{
URL + = Key + "=" + values [J] + "&";
}
}
Url = URL. trimend ('&');
}
Return URL;
}
}