MVC Easy Paging (Razor)

Source: Internet
Author: User

One, no data submitted

The first step is to create an empty controller named PageIndex, which customizes a method as follows:
Public ActionResult PageIndex (string Action, string controller, int currentpage, int pagecount)
{
int count = db. Product.count ();
Viewbag.pagecount = pagecount;//Gets the total number of data pages from the operation into the paging view page
Viewbag.currentpage = currentpage;//Gets the current number of pages from the Operation incoming Paging view page
Viewbag.action = action;
Viewbag.controller = Controller;
ReturnPartialview ();
}
Pass in four parameters:

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

    controller: Controllers;

    currentpage: The current number of pages;

    pagecount: Total pages of data

    Step Two: Add a view (PageIndex)

@if (Viewbag.pagecount = = NULL | | Viewbag.pagecount = = 0)
{
<span> Hello, there is no data display!</span>
}
Else
{
if (viewbag.currentpage <= 10)
{
<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}, nul L) ">
...</a> </span>

}
for (int i = viewbag.currentpage-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)" >
Page @i </a>|</span>
}
if (Viewbag.currentpage > 1)
{
<span><a href= "@Url. Action (Viewbag.action, Viewbag.controller, new {PageIndex = viewbag.currentpage-1}, NULL ) ">
Prev </a>|</span>
}
if (Viewbag.pagecount > Viewbag.currentpage)
{
<span><a href= "@Url. Action (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 (Viewbag.action, Viewbag.controller, new {PageIndex = Viewbag.currentpage + ten}, nul L) ">
...</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 operating view

Public ViewResult Index (int? pageIndex)
{
int pageind = Pageindex.hasvalue? Pageindex.value:1;
Viewbag.pagecount = (int) math.ceiling (result. Count ()/20.0);

Here is take, according to 20 per page display
return View (result. (t = t.pid). Skip ((pageInd-1) * 20). Take (20));
}

  Fourth Step: page call (i.e. last step)

@Html. Action ("PageIndex", "Product", new {Action = "Index", controller = "Log", PageCount = Viewbag.pagecount, Curren Tpage = viewbag.currentpage})

In general, data is variable.

Second, there is data submitted

&NBSP;&NBSP;&NBSP; The first step: Create an empty controller named PageIndex, and customize one of the following methods:
        public actionresult pageindexkey (int currentpage, int pagecount)
        {
             Viewbag.pagecount = pagecount;//The total number of pages fetched from the operation is passed into the paging view page
            viewbag.currentpage = currentPage;// Getting the current number of pages from an operation will pass through the paging view page
             return Partialview ();
       }

    Step Two: Create a distribution view

<script>
$ (function () {
$ ("#pageingByForm a"). Click (Function (event) {
$ ("#pageIndex"). Val ($ (this). attr ("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 no data </span>
}
Else
{
if (viewbag.currentpage <= 10)
{
<span><a pageindex= "1" href= "#" > Home </a>|</span>
}

Else
{
<span><a pageindex= "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 >1)
{
<span><a pageindex= "@ (viewbag.currentpage-1)" href= "#" > Prev </a>|</span>
}
if (Viewbag.pagecount > Viewbag.currentpage)
{
<span><a pageindex= "@ (viewbag.currentpage + 1)" href= "#" > Next </a></span>
}
if (viewbag.currentpage >= viewbag.pagecount-10)
{
}
Else
{
<span><a pageindex= "@ (viewbag.currentpage +)" href= "#" >...</a>|</span>
<span><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 the 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. (t = t.pid). Skip ((pageInd-1) * 20). Take (20));
}

View (page call):

@using (Html.BeginForm ())
{

Get query results based on gender

Gender: @Html. TextBox ("Sex")

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

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

}

Example:

Data, a collection of lists

   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, currentpage = viewbag.currentpage})

MVC Easy Paging (Razor)

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.