English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
1. No data submitted
First step, create an empty controller named PageIndex, and define a method as follows:
public ActionResult PageIndex(string action, string controller, int currentPage, int pageCount) } //int count = db.Product.Count(); ViewBag.PageCount = pageCount;//Get the total number of data pages from the operation and pass it to the pagination view page ViewBag.CurrentPage = currentPage;//Get the current page number from the operation and pass it to the pagination view page ViewBag.action = action; ViewBag.controller = controller; return PartialView(); }
Pass four parameters:
action: Operation (the operation of the view to be paginated, default is Index);
controller: Controller;
currentPage: Current page number;
pageCount: Total number of data pages
Second step:Add view (PageIndex)
@if (ViewBag.PageCount == null || ViewBag.PageCount == 0) } <span>Hello, no data is displayed at the moment!</span>/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 }, null)"> ...</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)"> Previous page</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 page</a> } else } <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 10 }, null)"> ...</a></span> <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)"> Last page</a> } <span style="padding-left: 20px">Current page number: @ViewBag.CurrentPage | Total @ViewBag.PageCount pages </span> }
Step 3:Modify the controller of the operation's 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 each page20 displayed return View (result.OrderBy (t => t.PID).Skip ((pageInd - 1) * 20).Take(20)); }
Fourth step:Page call (i.e., the final step)
@Html.Action("PageIndex", "Product", new { action = "Index", controller = "Log", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
Generally, data is dynamic.
Second, there is data submission
First step: Create an empty controller named PageIndex and define a method as follows:
public ActionResult PageIndexKey(int currentPage, int pageCount) } ViewBag.PageCount = pageCount;//Get the total number of data pages from the operation and pass it to the pagination view page ViewBag.CurrentPage = currentPage;//Get the current page number from the operation and pass it to the pagination view page return PartialView(); }
Second step:Establish 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>No data available</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="#">Page @i</a>|</span> } if (ViewBag.CurrentPage >1) } <span><a pageIndex="@(ViewBag.CurrentPage - 1)" href="#">Previous page</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 + 10)" href="#">...</a>|</span> <span><a pageIndex="@ViewBag.PageCount" href="#">Last page</a></span> } <span style="padding-left: 20px">Current page number: @ViewBag.CurrentPage | Total @ViewBag.PageCount pages </span> } </div>
Step 3:Modify view and controller operations
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.OrderBy (t => t.PID).Skip ((pageInd - 1) * 20).Take(20)); }
View (page call):
@using (Html.BeginForm())
}
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 a list List<string> s = new List<string>(); s.Add("张军"); ViewBag.PageCount = (int)Math.Ceiling(s.Count() / 20.0); return View(s.Skip((pageInd - 1) * 20).Take(20)); @Html.Action("PageIndex", "PageIndex", new { action = "", controller = "", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
That's all for this article. I hope it will be helpful to everyone's learning and that everyone will support the Yelling Tutorial more.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.