MVC 分页
public ActionResult Index(int id = 1, string Keyword = "") { ViewBag.Page_Count = 10; ViewBag.CurrentPageSize = id; ViewBag.pageIndex = id; ViewBag.KeyName = Keyword; ViewBag.Keyword = "&&keyword=" + Keyword; List<UserInfo> model = null ; if (Keyword.Length > 0) { model = db.userinfo.Where(user => user.UserName.IndexOf(Keyword) != -1).OrderByDescending(a => a.ID).Skip(10 * (id - 1)).Take(10).ToList(); ViewBag.pageCountAll = db.userinfo.ToList().Count; } else { //EF 使用SQL分页 var user = db.Database.SqlQuery<UserInfo>("select* from UserInfoes where id>@ID ",new SqlParameter("@ID",id)).ToList(); model = user.Skip(10 * (id - 1)).Take(10).ToList(); ViewBag.pageCountAll = user.Count; //结合linq分页 //var user = (from p in db.userinfo.SqlQuery("select * from UserInfoes", new SqlParameter("@ID", 1)) select p).ToList(); //ViewBag.pageCountAll = user.ToList().Count; //EF 分页 //var user = db.userinfo; //model = user.OrderByDescending(a => a.ID).Skip(10 * (id - 1)).Take(10).ToList(); // ViewBag.pageCountAll = user.ToList().Count; } return View(model); }
<script> var pageCountAll; var Page_Count; var CurrentPageSize; var pageIndex; var KeyName; var Keyword; $(function () { pageCountAll = "@ViewBag.pageCountAll"; Page_Count = "@ViewBag.Page_Count";//每页显示条数 CurrentPageSize = "@ViewBag.CurrentPageSize"//当前页; pageIndex = "@ViewBag.pageIndex";//第几页 KeyName = "@ViewBag.KeyName";//查询条件 Keyword = "@ViewBag.Keyword"; jsPage(KeyName, Keyword, 'divPage', pageCountAll, Page_Count, pageIndex, 'goPage'); }); function goPage(pageIndex) { jsPage(KeyName, Keyword,'divPage', pageCountAll, Page_Count, pageIndex, 'goPage'); CurrentPageSize = pageIndex; } </script>
CSS
/*分页样式*/ #divPage { text-align:left; margin:10px 0px; height:30px; font-size:12px; } #divPage a, #divPage span { text-decoration:none; color:Blue; background-color:White; padding:3px 5px; font-family:Consolas; text-align:center; border:solid 1px #ddd; display:inline-block; } #divPage span { color:gray; } #divPage a:hover { color:Red; } #divPage .aCur { background-color:green; color:White; font-weight:bold; }
//js分页
//el:分页容器 count:总记录数 pageStep:每页显示多少个 pageNum:第几页 fnGo:分页跳转函数 var jsPage = function (KeyName, Keyword,el, count, pageStep, pageNum, fnGo) { this.getLink = function (fnGo, index, pageNum, text) { var url = ""; if (KeyName != "") { url = Keyword; } //var s = '<a href="/Userinfo/index/' + index + '" onclick="' + fnGo + '(' + index + ');" '; var s; if (url != "") { s = '<a href="?id=' + index + ''+url+'" onclick="' + fnGo + '(' + index + ');" '; } else { s = '<a href="?id=' + index + '" onclick="' + fnGo + '(' + index + ');" '; } if (index == pageNum) { s += 'class="aCur" '; } text = text || index; s += '>' + text + '</a> '; return s; } Page_Count = pageStep var divPage = document.getElementById(el); //总页数 var pageNumAll = Math.ceil(count / pageStep); if (pageNumAll == 1 || pageNumAll == 0) { divPage.innerHTML = ""; return; } var itemNum = 5; //当前页左右两边显示个数 pageNum = Math.max(pageNum, 1); pageNum = Math.min(pageNum, pageNumAll); var s = '每页显示 <input id="txtajaxPage_PageSize3" style="width: 30px" type="text" onkeyup="ResetPageSize(this)" value=' + pageStep + ' />条数据 共 <span style=\" text-align:center;background-color :transparent; width: 51px; color: #ff0000; border-bottom: black 1px; vertical-align: middle; border-top-style: none; border-right-style: none; border-left-style: none;\">' + count + '</span> 条记录'; if (pageNum > 1) { s += this.getLink(fnGo, pageNum - 1, pageNum, '上一页'); } else { s += '<span>上一页</span> '; } var begin = 1; if (pageNum - itemNum > 1) { s += this.getLink(fnGo, 1, pageNum) + '... '; begin = pageNum - itemNum; } var end = Math.min(pageNumAll, begin + itemNum * 2); if (end == pageNumAll - 1) { end = pageNumAll; } for (var i = begin; i <= end; i++) { s += this.getLink(fnGo, i, pageNum); } if (end < pageNumAll) { s += '... ' + this.getLink(fnGo, pageNumAll, pageNum); } if (pageNum < pageNumAll) { s += this.getLink(fnGo, pageNum + 1, pageNum, '下一页'); } else { s += '<span>下一页</span> '; } divPage.innerHTML = s; }
视图页面
@using (Html.BeginForm("Deletes", "UserInfo", FormMethod.Post)) { <a href="/UserInfo/Create">添加用户</a> @Html.ActionLink("测试", "INDEX","HOME", new { Sid = 1,name="asdf" },null) <input type="text" id="txt_name" name="name" value="@ViewBag.KeyName" /> <input type="submit" value="查询" onclick="search();" /> <br /> <input type="button" value="批量删除" onclick="delIds()"/> <table> <tr> <th> <input type="checkbox" name="ckAll" id="ckAll" onclick="selectAll();" /><span id="spInfo">全选</span> </th> <th> 用户名 </th> <th> 电话 </th> <th> 邮箱 </th> <th> 注册时间 </th> <th> 操作 </th> </tr> @foreach (var item in Model) { <tr> <td> @Html.CheckBox("ckSelect", false, new { value = item.ID }) </td> <td> @item.UserName </td> <td> @item.Phone </td> <td> @item.Email </td> <td> @item.AddTime </td> <td> @Html.ActionLink("编辑", "Edit","userinfo", new { id = item.ID },null) <a href="javascript:void(0);" onclick="confirmDel(@item.ID);">删除</a> </td> </tr> } </table> } //分页DIV <div id="divPage"></div>