一:UserController 后臺(tái)查選數(shù)據(jù)
1.獲取指定頁(yè)數(shù)據(jù)?
//PageSize代表一頁(yè)有多少條數(shù)據(jù)叶摄,PageNo代表當(dāng)前頁(yè)數(shù)
?public List<User> Select(int PageSize, int PageNo)?
?{?
?????????var db = new Context();?
?????????if (PageNo<=1) { PageNo = 1; }?
?????????List ulist = db.Users.Include(x => x.AuthorityRs).OrderBy(a => a.Id).Skip((PageNo - 1) * PageSize).Take(PageSize).ToList();//查選一頁(yè)的數(shù)據(jù)
? ? ? ? ? ? return ulist;
? ? ? ? }
2.獲取總頁(yè)數(shù)
? ? ? ? public int GetPageCount(int pageSize)//PageSize代表一頁(yè)有多少條數(shù)據(jù)
? ? ? ? {
? ? ? ? ? ? var db = new Context();
? ? ? ? ? ? int rowsCount = db.Users.Count();//總數(shù)據(jù)
? ? ? ? ? ? int pageCount = (int)Math.Ceiling(1.0 * rowsCount / pageSize);//取天花板數(shù)
? ? ? ? ? ? return pageCount;//頁(yè)碼數(shù)
? ? ? ? }
詳解:public int pageSizeCount = 5;//選擇一頁(yè)為5條數(shù)據(jù)
3.?Index函數(shù)
public ActionResult Index(int? PageNo)
?{
? ??????ViewBag.pageSize = pageSizeCount;
? ? ? ? ? ? if (PageNo== null||PageNo<=0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? id = 1;/如果所傳PageNo為或者小于0時(shí),賦值為1
? ? ? ? ? ? }
? ??????????List list =Select(pageSizeCount, id.Value);//找出當(dāng)前數(shù)據(jù)
????????????int pageCount =GetPageCount(pageSizeCount);//找出總頁(yè)數(shù)
????????????int nextPageNo = id.Value >= pageCount ? pageCount : id.Value + 1;//計(jì)算下一頁(yè)頁(yè)號(hào)
????????????int prevPageNo = id.Value == 1 ? 1 : id.Value - 1;//計(jì)算上一頁(yè)頁(yè)號(hào)
????????????//使用viewbag帶到視圖去
? ? ? ? ? ? ViewBag.NextPageNo = nextPageNo;
? ? ? ? ? ? ViewBag.PrevPageNo = prevPageNo;
? ? ? ? ? ? ViewBag.PageCount = pageCount;//總頁(yè)數(shù)
? ? ? ? ? ? ViewBag.PageNo = id;//當(dāng)前頁(yè)號(hào)
????????//下拉列表顯示頁(yè)數(shù)需要的selectlist數(shù)據(jù) ListlistPage = new List();
? ? ? ? ? ? for (int i = 1; i <= pageCount; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ????listPage.Add(i);
? ? ? ? ? ? }
? ? ? ? ? ? SelectList li = new SelectList(listPage, id);
? ? ? ? ? ? ViewBag.PageList = li;
? ? ? ? ? ? return View(list);
}
2.前臺(tái)顯示:
共 @ViewBag.PageCount 頁(yè)安拟,當(dāng)前是第 @ViewBag.PageNo 頁(yè)
?@Html.ActionLink("首頁(yè)", "Index", new { PageNo = 1 })
?@Html.ActionLink("上一頁(yè)", "Index", "Process", new { PageNo = (int)ViewBag.PageNo - 1 }, null) @Html.ActionLink("下一頁(yè)", "Index", "Process", new { PageNo = (int)ViewBag.NextPageNo }, null) @Html.ActionLink("尾頁(yè)", "Index", new { PageNo = (int)ViewBag.PageCount })
@using (Html.BeginForm("Index", "Process"))? ? {? ? ? ? ? ? ??
? @:轉(zhuǎn)到:@Html.DropDownList("PageNo", (SelectList)ViewBag.Pagelist)
}