AI智能
改变未来

asp.net 分页

asp.net 分页

  • ==控制==
  • ==视图==

控制

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using Com.Hi.FY.Models;namespace Com.Hi.FY.Controllers{public class RoleController : Controller{// GET: RoleRbacDBEntities db = new RbacDBEntities();public ActionResult Index(int pageindex=1,int pagesize=10,string Name=\"\")   //pageindex 页数  //pagesize 每页显示多少条{//总记录数满足条件var counts = db.Roles.Where(p => p.Name.Contains(Name)).Count();var totalPage = Math.Ceiling(counts * 1.00 / pagesize);ViewBag.totalPage = totalPage;//根据页码的条数查询var a = 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(a);}}}

视图

@{ViewBag.Title = \"Index\";}@using Com.Hi.FY.Models@model List<Role><div style=\"display:flex; justify-content:space-between\"><div class=\"btn-group\" style=\"float:left\"><button type=\"button\" class=\"btn btn-default\">新增</button><button type=\"button\" class=\"btn btn-default\">删除</button></div><div class=\"input-group\"><label>名字</label><input type=\"text\" id=\"txtCongName\" value=\"@ViewBag.Name\"/><input type=\"button\" value=\"搜索\" id=\"btnSearch\" onclick=\"page(1);\" /></div></div><table class=\"table table-bordered\"><thead><tr><th>编号</th><th>名字</th><th>备注</th></tr></thead><tbody>@foreach (var item in Model){<tr><td>@item.ID</td><td>@item.Name</td><td>@item.Remark</td></tr>}</tbody></table><nav aria-label=\"Page navigation\" style=\"display:flex;justify-content:space-between\"><ul class=\"pagination\"><li><a href=\"#\">共10页,第<input type=\"text\" value=\"1\" id=\"pageindex\" />页,每页显示<select id=\"pagesize\" onchange=\"page(1)\">@{var pageindexs = new List<int> { 5, 10, 20, 50 };foreach (var item in pageindexs){if (ViewBag.pagesize == item){<option value=\"@item\" selected=\"selected\">@item</option>}else{<option value=\"@item\">@item</option>}}}</select>条</a></li></ul><ul class=\"pagination\"><li><a href=\"javascript:page(1)\">首页</a></li>             @*调脚本 javescript*@@if (@ViewBag.pageindex > 1){<li><a href=\"javascript:page(@ViewBag.pageindex-1)\">上页</a></li>}else{<li><a href=\"javascript:page(@ViewBag.pageindex-1)\" class=\"disabled\">上页</a></li>}<li><a href=\"javascript:page(@ViewBag.pageindex+1)\">下页</a></li><li><a href=\"javascript:page(@ViewBag.totalPage)\">尾页</a></li><li><input type=\"button\" value=\"go\" onclick=\"go();\" /></li></ul></nav>@section scripts{<script>function page(pageindex) {var pageSize = $(\"#pagesize\").val();var name = $(\"#txtCongName\").val();window.location.href = \"/role/index?pageindex=\" + pageindex + \"&pagesize=\" + pageSize + \"&name=\" + name;    /*location 网页地址*/}function go() {//获取pageindexvar pageindex = $(\"#pageindex\").val();page(pageindex);}</script>}

		
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » asp.net 分页