AI智能
改变未来

ASP.NET中如何使用EF框架+MVC进行分页操作

文章目录

  • 使用EF框架和MVC进行分页的好处
  • 案例演示

使用EF框架和MVC进行分页的好处

方便快捷,代码量少。

案例演示

话不多说,直接上效果图

我们以Roles表为例,先看看表里的数据

控制器代码:

public ActionResult Index(int pageIndex=1,int pageSize=10,string name=\"\"){//获取总记录数var totalRows = db.Roles.Count();//计算总页数var totalPages = Math.Ceiling(totalRows * 1.00 / pageSize);ViewBag.totalPages = totalPages;var roles = db.Roles.Where(p=>p.Name.Contains(name)).OrderBy(p=>p.ID)  //排序.Skip((pageIndex-1) * pageSize)    //跳过.Take(pageSize)   //取.ToList();ViewBag.pageIndex = pageIndex;ViewBag.name = name;ViewBag.pageSize = pageSize;return View(roles);}

视图代码:

@{ViewBag.Title = \"Index\";}<h2>角色列表</h2><div class=\"input-group\"><label>名称:</label><input type=\"text\" class=\"form-control\" id=\"txtCondName\" value=\"@ViewBag.name\"/><input type=\"button\" id=\"btnSearch\" value=\"搜索\" οnclick=\"page(1);\" /></div><table class=\"table table-bordered\"><thead><tr><th>编号</th><th>名称</th><th>备注</th></tr></thead><tbody>@foreach (var item in Model){<tr><th>@item.ID</th><th>@item.Name</th><th>@item.Remark</th></tr>}</tbody></table><nav aria-label=\"...\"><ul class=\"pagination\"><li class=\"page-item \">共 @ViewBag.totalPages 页,第<input type=\"number\" min=\"1\" max=\"@ViewBag.totalPages\" id=\"pageIndex\" value=\"@ViewBag.pageIndex\" />页,每页显示<select id=\"pageSize\" οnchange=\"page(1);\">@{var listPageSize = new List<int> { 5, 10, 20, 50, 100 };}@foreach (var item in listPageSize){if (@ViewBag.pageSize == @item){<option value=\"@item\" selected=\"selected\">@item</option>}else{<option value=\"@item\">@item</option>}}</select>条</li></ul><ul class=\"pagination\"><li class=\"page-item \"><a class=\"page-link\" href=\"javascript:page(1);\" tabindex=\"-1\" aria-disabled=\"true\">首页</a></li>@if (@ViewBag.pageIndex > 1){<li class=\"page-item\"><a class=\"page-link\" href=\"javascript:page(@ViewBag.pageIndex-1);\">上页</a></li>}else{<li class=\"page-item disabled\"><a class=\"page-link\" href=\"javascript:page(@ViewBag.pageIndex-1);\">上页</a></li>}<li class=\"page-item \" aria-current=\"page\"><span class=\"page-link\"> @ViewBag.pageIndex</span></li>@if (@ViewBag.pageIndex < @ViewBag.totalPages){<li class=\"page-item \" aria-current=\"page\"><a class=\"page-link\" href=\"javascript:page(@ViewBag.pageIndex+1);\">下页 </a></li>}else{<li class=\"page-item disabled\" aria-current=\"page\"><a class=\"page-link\" href=\"javascript:page(@ViewBag.pageIndex+1);\">下页 </a></li>}<li class=\"page-item\"><a class=\"page-link\" href=\"javascript:page(@ViewBag.totalPages);\">末页</a></li><li class=\"page-item\"><input type=\"button\" class=\"page-link\" value=\"GO\" οnclick=\"go()\" /></li></ul></nav>@section scripts{<script>function page(pageIndex) {var name = $(\"#txtCondName\").val(); //获取搜索框的值var pageSize = $(\"#pageSize\").val(); //获取下拉框的值window.location.href = \"/role/index?pageindex=\" + pageIndex+ \"&pageSize=\" + pageSize + \"&name=\" + name;}function go() {//获取输入的页码的值var pageIndex = $(\"#pageIndex\").val();page(pageIndex);}</script>}
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » ASP.NET中如何使用EF框架+MVC进行分页操作