//生成一个存储的类public class pageModel{//信息结果集public List<\"显示的结果集\"> list { get; set; }//当前页public int currentPage { get; set; }//总记录数public int totalCount { get; set; }//总页数public int totalPage { get; set; }}
API中实现Linq语句分页
public pageModel Get(int pageSize = 3, int currentPage = 1){//修正传入的页数<1的情况if (currentPage < 1){currentPage = 1}var list=\"要显示分页数据的结果集\";var count = list.Count(); //查询出总记录数//查询出总页数int page;if (count % pageSize == 0){page = count / pageSize;}else{page = count / pageSize + 1;}//纠正当前页 > 总页数的情况if (currentPage > page){currentPage = page;}//使用linq分页查询列表list = list.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();var m = new pageModel();m.list = list; //列表就是分页查询出的列表m.totalCount = count;m.totalPage = page;m.currentPage = currentPage;return m;}
MVC中生成视图
分页控件表单+Jquery
<!--分页控件--><table class=\"table\"><tr><td>总共<span id=\"totalCount\"></span>条记录</td><td>第<span id=\"currentPage\"></span>页</td><td>共<span id=\"totalPage\"></span>页</td><td><a href=\"javascript:;\" onclick=\"show(1)\">首页</a></td><td><a href=\"javascript:;\" onclick=\"show(currentPage-1)\">上一页</a></td><td><a href=\"javascript:;\" onclick=\"show(currentPage+1)\">下一页</a></td><td><a href=\"javascript:;\" onclick=\"show(totalPage)\">尾页</a></td></tr></table><script>$(function () {show(1);});var currentPage = 1;var totalPage = 1;function show(page) {var obj = {};obj.pageSize = 3;obj.currentPage = page;$.ajax({url: \"http://localhost:53967/api/GetPage\",type: \"get\",data: obj,success: function (result) {$(\"#totalCount\").html(result.totalCount);$(\"#totalPage\").html(result.totalPage);$(\"#currentPage\").html(result.currentPage);currentPage = result.currentPage;totalPage = result.totalPage;$(result.list).each(function () {//拼接字符串}});}</script>