asp.net MVC4 razor Template Easy Paging effect _ practical skills

Source: Internet
Author: User

I. No data submission

The first step is to create an empty controller named PageIndex Controller, which is customized as follows:

    Public ActionResult PageIndex (string action, string controller, int currentpage, int pagecount)
    {
      //int count = db . Product.count ();
      Viewbag.pagecount = pagecount;//The total number of pages from the operation will pass in the Paging view page
      viewbag.currentpage = currentpage;//Get the current number of pages from the operation will pass in the paging view page
      viewbag.action = action;
      Viewbag.controller = Controller;
      return Partialview ();
    }

Pass in four parameters:

Action: Operation (the operation of the view to be paginated, the default is index);

Controller: Controller;

CurrentPage: current page number;

PageCount: Total pages of data

Step Two: add view (PageIndex)

@if (Viewbag.pagecount = NULL | | Viewbag.pagecount = = 0 {<span> Hello, no data currently displayed!</span>} else {if (viewbag.currentpa GE <= {<span><a href= "@Url. Action (Viewbag.action, Viewbag.controller, new {PageIndex = 1}, NULL "> Home </a>|</span>} else {<a href=" @Url. Action (Viewbag.action, Viewbag.controller, new {PageIndex = 1}, NULL) "> Home </a> <span><a href=" @Url. Action (Viewbag.action, Viewbag.controller, New {PageIndex = viewbag.currentpage-10}, NULL) "> ...</a> </span>} for (int i = Viewbag.curr entPage-3; I < Viewbag.currentpage + 3;
    i++) {if (i <= 0) {continue;
    } if (i > Viewbag.pagecount) {break; <span><a href= "@Url. Action (Viewbag.action, Viewbag.controller, new {PageIndex = i}, null)" > @i page &l T;/a>|</span>} if (Viewbag.currentpage > 1) {<span><a href= "@Url. Action (Viewbag.action, Viewbag.controller, new {PageIndex = viewbag.currentpage-1}, null)" > Previous Page </a>|</span>} if (Viewbag.pagecount > Viewbag.currentpage) {<span><a href= "@Url. Act Ion (Viewbag.action, Viewbag.controller, new {PageIndex = Viewbag.currentpage + 1}, NULL) "> next page </a></span >} if (viewbag.currentpage = Viewbag.pagecount | |  Viewbag.currentpage >= viewbag.pagecount-10) {<a href= "@Url. Action (Viewbag.action, Viewbag.controller, new {PageIndex = Viewbag.pagecount}, NULL] "> Last </a>} else {<span><a href=" @Url. Action (View
  Bag.action, Viewbag.controller, new {PageIndex = Viewbag.currentpage +}, NULL) "> ...</a></span> <a href= "@Url. Action (Viewbag.action, Viewbag.controller, new {PageIndex = Viewbag.pagecount}, NULL)" > Last </ a>} <span style= "padding-left:20px" > Current page: @ViewBag. CurrentPage | Total @ViewBag.pagecount Page </span>}

 

Step Three: controller modification of the view of the operation

Public Viewresult Index (int. PageIndex)
{
int pageind = pageindex.hasvalue? pageindex.value:1;
 Viewbag.pagecount = (int) math.ceiling (result.      Count ()/20.0);

Here's the take, which shows return View per page 20
. By (T => t.pid). Skip ((pageInd-1) * 20).    Take ());
}

Fourth Step: page Call (that is, last step)

Copy Code code as follows:
@Html. Action ("PageIndex", "Product", new {action = "Index", controller = "Log", PageCount = Viewbag.pagecount, Currentpag e = viewbag.currentpage})

In general, the data are variable.

II. Submission of data

Step One: Create an empty controller named PageIndex Controller, and customize a method as follows:

    Public ActionResult pageindexkey (int currentpage, int pagecount)
    {
      Viewbag.pagecount = pagecount;// Getting total number of pages from an operation will pass in the Paging view page
      viewbag.currentpage = currentpage;//Get the current number of pages from an operation will pass in the Paging view page return
      Partialview ();
    }

Step Two: Create a distribution view

 <script> $ (function () {$ (' #pageingByForm a '). Click (Function (Event) {$ (#pageIndex). Val ($ (this).
      TR ("PageIndex"));
      $ (this). Parent ("Form"). Submit ();
      document.getElementsByTagName ("Form"). Item (0). Submit ();
    Event.preventdefault ();
  });
}); </script> @Html. Hidden ("PageIndex") <div id= "Pageingbyform" > @if (viewbag.pagecount = NULL | | 
    Viewbag.pagecount = = 0) {<span> currently has no data </span>} else {if (viewbag.currentpage <= 10) { <span><a pageindex= "1" href= "#" > Home </a>|</span>} else {<span><a PA geindex= "1" href= "#" > Home </a>|</span> <span><a pageindex= "@ (viewbag.currentpage-10)" Href= "# ' >...</a>|</span>} for (int i = viewbag.currentpage-3; I < Viewbag.currentpage + 3;
      i++) {if (i <= 0) {continue;
 } if (i > Viewbag.pagecount) {break;     } <span><a pageindex= "@i" href= "#" > @i page </a>|</span>} if (Viewbag.currentpage &
    gt;1) {<span><a pageindex= @ (viewbag.currentpage-1) href= "#" > Prev </a>|</span>} if (Viewbag.pagecount > Viewbag.currentpage) {<span><a pageindex= @ (viewbag.currentpage + 1) "href="
    # "> next page </a></span>} if (Viewbag.currentpage >= viewbag.pagecount-10) {} else {<span><a pageindex= "@ (viewbag.currentpage +)" href= "#" >...</a>|</span> &LT;SPAN&GT;&L T;a pageindex= "@ViewBag. PageCount" href= "#" > Last </a></span>} <span style= "padding-left:20px" ; current page: @ViewBag. CurrentPage |

 Total @ViewBag. PageCount Page </span>} </div>

Step Three: Modify Operation View and controller

Public Viewresult Index (int? PageIndex, string search)
{
int pageind = pageindex.hasvalue? Pageindex.value:  1;
 Viewbag.pagecount = (int) math.ceiling (result.  Count ()/20.0); 
return View (result. By (T => t.pid). Skip ((pageInd-1) * 20).  Take ());
}


View (page call):
@using (Html.BeginForm ())
{

Query results based on sex

Sex: @Html. TextBox ("Sex")

<input type= "Submit" value= "Query"/>

@Html. Action ("Pageindexkey", "PageIndex", new {PageCount = Viewbag.pagecount, currentpage = viewbag.currentpage})

}

Example:

Data, the set of a List 
list<string> s = new list<string> (); 
      S.add ("Zhang June"); 
      Viewbag.pagecount = (int) math.ceiling (S.count ()/20.0); 
      Return View (S.skip (pageInd-1) * 20).    Take ()); 
@Html. Action ("PageIndex", "PageIndex", 
new {action = "", Controller = "", PageCount = Viewbag.pagecount, CURRENTP Age = Viewbag.currentpage})

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.