Sample Code for implementing paging query using ASP. NET MVC5, asp. netmvc5

Source: Internet
Author: User

Sample Code for implementing paging query using ASP. NET MVC5, asp. netmvc5

Paging is a good choice for querying and displaying large amounts of data. This article briefly introduces how to implement paging query.

Three variables are required for paging: total data volume, number of data entries displayed on each page, and current page number.

// Total data volume int dataCount; // The number of data entries displayed per page int pageDataCount; int pageNumber;

The total number of pages is calculated based on the total data volume and the number of data entries displayed per page. The starting and ending line numbers for reading data from the database are calculated based on the current page number and the number of data entries displayed per page.

// Total number of pages int pageCount = (int) Math. ceiling (dataCount/(pageDataCount * 1.0); int startLine = (pageNumber-1) * PageDataCount + 1; int endLine = startLine + PageDataCount-1;

The lightweight ORM framework Dapper is used for database query operations. The specific code is as follows:

Protected IDbConnection CreateConnection () {IDbConnection dbConnection = new SqlConnection (ConnectionString); dbConnection. open (); return dbConnection;} // obtain the total number of data entries in the database public virtual int QueryDataCount (string tableName) {using (IDbConnection dbConnection = CreateConnection ()) {var queryResult = dbConnection. query <int> ("select count (Id) from" + tableName); if (queryResult = null |! QueryResult. any () {return 0;} return queryResult. first () ;}} public virtual IEnumerable <T> RangeQuery <T> (string tableName, int startline, int endline) {if (string. isNullOrEmpty (tableName) {throw new ArgumentNullException ("table name cannot be blank or null");} if (startline <= 0) {throw new ArgumentOutOfRangeException ("the starting line number must be greater than 0");} if (endline-startline <0) {throw new ArgumentOutOfRangeException ("the ending line number must not be less than the starting line number. ");} Using (IDbConnection dbConnection = CreateConnection () {var queryResult = dbConnection. query <T> ("select top" + (endline-startline + 1) + "* from" + tableName + "where Id not in (select top" + (startline-1) + "Id from" + tableName + "order by Id desc) order by Id desc"); if (queryResult! = Null & queryResult. Any () {return queryResult ;}} return null ;}

Pagination button

Add PageHelper in the App_Code folder. the cshtml file encapsulates the code of the draw button. Note that when you use VS to publish a site, the files in the App_Code folder will not be packaged. You need to manually copy the files in the App_Code folder to the site.

@ * Amount: total data, count: the number of data entries displayed on each page. You must reference bootstrap.min.css on the jump link page when you click the redierctUrl button * @ helper CreatePaginateButton (int amount, int count, string redirectUrl) {<div id = "pagenumber" style = "position: fixed; bottom:-15px; text-align: center; width: 84% "> <nav style =" text-align: center "> <ul class =" pagination "> <li> <a href =" @ redirectUrl/1 "rel =" external nofollow "> homepage </a> </li> @ {int pageNumber = (int) math. ceiling (amo Unt/(count * 1.0); pageNumber = pageNumber <1? 1: pageNumber; // The number of buttons displayed on the page (excluding buttons such as homepage, last page, previous page, and next page ), if the total number of pages exceeds this value, draw the button separator const int BUTTON_COUNT = 7; // button separator const string BUTTON_SEPARATOR = "...... "; // The number of buttons on the left side of the button separator (excluding the home page, last page, previous page, and next page buttons) const int LEFT_BUTTON_COUNT = 4; // The number of buttons on the right of the button separator (excluding the home page, last page, Previous Page, next page, and other buttons) const int RIGHT_BUTTON_COUNT = 2; string [] urlSegments = Request. url. segments; int selectedIndex = 0; int. tryParse (urlSegments [urlSegments. length-1], o Ut selectedIndex); int previous = (selectedIndex-1) <= 0? 1: selectedIndex-1; int next = (selectedIndex + 1> pageNumber )? PageNumber: selectedIndex + 1; var r = Request. cookies [""]; if (pageNumber> BUTTON_COUNT) {<li> <a id = "next" href = "@ redirectUrl/@ previous" rel = "external nofollow"> previous page </a> </li> for (int I = 1; I <= BUTTON_COUNT; I ++) {if (selectedIndex> = LEFT_BUTTON_COUNT & selectedIndex % LEFT_BUTTON_COUNT = 0 & I <= LEFT_BUTTON_COUNT) {<li> <a name = "pageButton" id = "@ selectedIndex" href = "@ redirectUrl/@ selectedI Ndex "rel =" external nofollow "> @ selectedIndex </a> </li> int step = selectedIndex; int tag = 0; for (I = 1; I <= LEFT_BUTTON_COUNT; I ++) {tag = I + step; if (tag> pageNumber-RIGHT_BUTTON_COUNT) {if (I <= LEFT_BUTTON_COUNT) {I = LEFT_BUTTON_COUNT + 1;} break ;} <li> <a name = "pageButton" id = "@ tag" href = "@ redirectUrl/@ tag" rel = "external nofollow" rel = "external nofollow"> @ tag </a> </li >}} else if (I <= LEFT_BUTTON_COUNT & selectedIndex <LEFT_BUTTON_COUNT) {<li> <a name = "pageButton" id = "@ I" href = "@ redirectUrl/@ I" rel = "external nofollow" rel = "external nofollow"> @ I </a> </li>} else if (I <LEFT_BUTTON_COUNT & selectedIndex> LEFT_BUTTON_COUNT) {int step = selectedIndex/LEFT_BUTTON_COUNT; int tag = 0; <li> <a name = "pageButton" id = "@ (step * LEFT_BUTTON_COUNT) "href =" @ redirectUrl/@ (step * LEFT_BUT TON_COUNT) "rel =" external nofollow "> @ (step * LEFT_BUTTON_COUNT) </a> </li> for (I = 1; I <= LEFT_BUTTON_COUNT; I ++) {tag = I + step * LEFT_BUTTON_COUNT; if (tag> pageNumber-RIGHT_BUTTON_COUNT) {if (I <= LEFT_BUTTON_COUNT) {I = LEFT_BUTTON_COUNT + 1;} break ;} <li> <a name = "pageButton" id = "@ tag" href = "@ redirectUrl/@ tag" rel = "external nofollow" rel = "external nofollow"> @ tag </a> </li >}// draw button Separator Right button if (I = BUTTON_COUNT-1) {<li> <a name = "pageButton" id = "@ (pageNumber-1)" href = "@ redirectUrl/@ (pageNumber-1) "rel =" external nofollow "> @ (pageNumber-1) </a> </li>} else if (I = BUTTON_COUNT) {<li> <a name = "pageButton" id = "@ pageNumber" href = "@ redirectUrl/@ pageNumber" rel = "external nofollow" rel = "external nofollow"> @ pageNumber </a> </li>} // draw the button separator else if (I >= BUTTON_COUNT-RIGHT_BUTTON_COUNT) {<li> <Span name = "pageButton"> @ BUTTON_SEPARATOR </span> </li >}< li> <a id = "next" href = "@ redirectUrl/@ next" rel = "external nofollow"> next page </a> </li>} else {for (int I = 1; I <= pageNumber; I ++) {<li> <a name = "pageButton" id = "@ I" href = "@ redirectUrl/@ I" rel = "external nofollow" rel = "external nofollow"> @ I </a> </li >}}< li> <a href = "@ redirectUrl/@ pageNumber" rel = "external nofollow" rel = "external nofollow"> Last page </a> </li> </ul> </nav> </div> <script> $ (function () {// sets the selected button's background color var selected =$ ('# @ selectedIndex'); if (selected! = Undefined) {selected.css ('background-color', '# E1E1E1');} </script>}

Call the front-end page to draw the paging button.

@PageHelper.CreatePaginateButton(246, 10, "/usermanager/attentionlist/")

The following are several paging buttons:



Corresponding HTML code:


The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.