1.同步分页:
1.准备工作: 在NuGet中下载PagedList.Mvc
2.在Views 中的 web.config 中的namespaces中添加
<add namespace="PagedList"/>
<add namespace="PagedList.Mvc"/>
3.控制器代码:先using PagedList
public class HomeController : Controller { private dbcontext db = new dbcontext(); public ActionResult Index (int page = 1) { var data = db.ajax.OrderBy(o => o.id);//必须排序 return View(data.ToPagedList(page,10); } }
4.Index页面
@model IPagedList<ajax异步.Models.ajax> @{ Layout = "~/Views/Shared/_Layout.cshtml"; } <script src="~/Scripts/jquery-3.3.1.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> @Html.PagedListPager(Model, page => Url.Action("Index", "Home", new { page=page })) <table> <tr> <td>id</td> <td>name</td> </tr> @foreach (var item in Model) { <tr> <td>@Html.DisplayFor(c=>item.id)</td> <td>@Html.DisplayFor(c=>item.name)</td> </tr> } </table>
主要是用到了@Html.PagedListPager()