1.后台Controller
public ActionResult Index(int pageIndex = 1, int pageSize = 3) { MyDataDBEntities db = new MyDataDBEntities(); //获取集合总数 int userListCount = db.User.Count(); //获取页数总计,计算:拿集合总数除以当前页面显示的条数的余数,如果余数为0,表示整除,则拿集合总数除以页面显示条数。如果余数不为0,则补充多加一页 int pageCount = userListCount % pageSize == 0 ? userListCount / pageSize : (userListCount / pageSize) + 1; //先根据Id排序再跳过 当前页面显示数目 * 当前显示页数 - 1,再拿出当前显示的数目。假如当前第一页,则3 * (1-1)= 0 ,跳过0条数据,取出3条数据。 var userList = db.User.OrderBy(o => o.Id).Skip(pageSize * (pageIndex - 1)).Take(pageSize); //传递给前端总页数 ViewBag.pageCount = pageCount; //传递给前端当前页数 ViewBag.pageIndex = pageIndex; return View(userList); //传出当前实例对象 }
2.前台代码
@if (pageCurrent <= 1) { @Ajax.ActionLink("上一页", "Index", "Home", new { pageIndex = 1, pageSize = 3 }, new AjaxOptions{ UpdateTargetId = "myTab"}, new {@class = "btn btn-success"}) // @Html.ActionLink("上一页", "Index", "Home", new {}, new { @class = "btn btn-primary" }) } else { @Ajax.ActionLink("上一页", "Index", "Home", new { pageIndex = pageCurrent - 1, pageSize = 3 }, new AjaxOptions{ UpdateTargetId = "myTab"}, new { @class = "btn btn-success" }) // @Html.ActionLink("上一页", "Index", "Home", new { pageIndex = pageCurrent - 1, pageSize = 3 }, new { @class = "btn btn-primary" }) } <span>第 @pageCurrent 页/ 共 @pageAll 页</span> @if (pageCurrent >= pageAll) { @Ajax.ActionLink("下一页","Index", "Home", new {pageIndex = pageAll, pageSize = 3}, new AjaxOptions{UpdateTargetId = "myTab"}, new { @class = "btn btn-success" }) // @Html.ActionLink("下一页", "Index", "Home", new { pageIndex = pageAll, pageSize = 3 }, new { @class = "btn btn-primary" }) } else { @Ajax.ActionLink("下一页","Index", "Home", new {pageIndex = pageCurrent + 1, pageSize = 3}, new AjaxOptions{UpdateTargetId = "myTab"}, new { @class = "btn btn-success" }) // @Html.ActionLink("下一页", "Index", "Home", new { pageIndex = pageCurrent + 1, pageSize = 3 }, new { @class = "btn btn-primary" }) }