Add Class PageBar.cs
Using system;using system.collections.generic;using system.linq;using system.text;using System.Web;namespace system.web.mvc{public static class Pagebar {public static htmlstring showpagenavigate (this htmlhelper HTMLH Elper, int currentpage, int pageSize, int totalcount) {var output = new StringBuilder (); var redirectto = HtmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath; PageSize = PageSize = = 0? 10:pagesize; var totalpages = Math.max ((totalcount + pageSize-1)/pageSize, 1); Total pages int maxpagercount = 10; int visiblestartmax=1; if (TotalPages > Maxpagercount) {visiblestartmax = TotalPages-(MaxPagerCount-1); } int visiblestart = currentpage-maxpagercount/2;//Start Page if (Visiblestart < 1) { Visiblestart = 1; } if (Visiblestart > Visiblestartmax) {Visiblestart = Visiblestartmax; } int visibleend = Visiblestart + maxpagercount-1;//End page//If the calculated end page number is greater than the total page number, adjust to the maximum page number if (Vis Ibleend > TotalPages) {visibleend = TotalPages; } if (currentpage = = 1) {output. Append ("<span> home </span>"); Output. Append (""); Output. Append ("<span> prev </span>"); Output. Append (""); } if (CurrentPage > 1) {output. AppendFormat ("<a class= ' pagelink ' href= ' {0}?pageindex=1&pagesize={1} ' > Home </a>", Redirectto, PageSize); Processes the connection output of the previous page. AppendFormat ("<a class= ' pagelink ' href= ' {0}?pageindex={1}&pagesize={2} ' > Prev </a>", Redirectto, CurrentPage-1, pageSize); } output. Append (""); Draw a visual page Number link for (int i = VisiblesTart; I <= visibleend; i++) {//The current page is not a hyperlink if (i = = currentpage) {OUTPU T.append ("<span>"). Append (i). Append ("</span>"); } else//General page processing {output. AppendFormat ("<a class= ' pagelink ' href= ' {0}?pageindex={1}&pagesize={2} ' >{3}</a>", Redirectto, I, PageSize, i); } output. Append (""); } if (CurrentPage < totalpages) {output. AppendFormat ("<a class= ' pagelink ' href= ' {0}?pageindex={1}&pagesize={2} ' > Next </a>", Redirectto, CurrentPage + 1, pageSize); Output. Append (""); Output. AppendFormat ("<a class= ' pagelink ' href= ' {0}?pageindex={1}&pagesize={2} ' > End </a>", Redirectto, TotalPages, pageSize); Output. Append (""); } else {output. ApPend ("<span> next page </span>"); Output. Append (""); Output. Append ("<span> end </span>"); Output. Append (""); } output. AppendFormat ("{0} page/total {1} page", CurrentPage, totalpages);//This statistic adds no to the return new htmlstring (output. ToString ()); } }}
Add controller Ajaxcontroller
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using Mvcapplicationstudy.models;namespace mvcapplicationstudy.controllers{public class Ajaxcontroller:controller { GET:/ajax/public actionresult Index () {return View (); }//Paging public ActionResult showuserinfolist () {testentities db = new testentities (); int pageIndex; if (!int. TryParse (request["PageIndex"],out PageIndex)) {pageIndex = 1; } int pageSize = 1; int totalcount = db. Userinfo.count (); int pagecount = Convert.ToInt32 (math.ceiling ((double) totalcount/pagesize)); if (PageIndex < 1) PageIndex = 1; if (PageIndex > PageCount) pageIndex = PageCount; var userinfolist = db. userinfo.where<userinfo> (U = True). By<userinfo, int> (U = u.id). Skip<userinfo> ((pageIndex-1) * pageSize). Take<userinfo> (pageSize); list<userinfo> list = Userinfolist.tolist (); Viewdata.model = list; Viewbag.pageindex = PageIndex; Viewbag.pagesize = PageSize; Viewbag.pagecount = PageCount; Viewbag.totalcount = TotalCount; return View (); } }}
Add View showuserinfolist
@model ienumerable<mvcapplicationstudy.models.userinfo>@{Layout = null;} <! DOCTYPE html>Run results
mvc--Sub-page