如图:

1: 控制器代码

//
// GET: /AjaxUser/
shopEntities shop = new shopEntities();
public ActionResult Index()
{
return View();
} public ActionResult loadjson()
{
int pageSize = Request["pageSize"] == null ? 10 : int.Parse(Request["pageSize"]);
int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);
int totalCount = shop.tbl_admin.Count(); //给前台userinfo所有的数据,并且是json格式
var allorder = shop.tbl_admin.OrderBy(u=>u.id)
.Skip(pageSize*(pageIndex-1))
.Take(pageSize)
.ToList();
//接受一个对像,内部把此对象使用javaScript序列化类对象志字符串,发送到前台 var data = from u in allorder select new { u.id,u.realname,u.sex}; string strNav = PageNavHelper.ShowPageNavigate(pageIndex,pageSize,totalCount); var result = new {Data=data, NavStr=strNav };
return Json(result, JsonRequestBehavior.AllowGet);
}

  

2:Html代码

@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" /> <title>Index</title>
<link href="~/Content/NavPage.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.8.24.js"></script>
<script src="~/Scripts/jquery.easyui.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script> <script type="text/javascript">
$(function () {
//页面加载完成后从后如加载当前页数据
initTable();
}); //初始化表格数据
function initTable(queryData)
{
$.getJSON("/AjaxUser/loadjson",queryData, function (data) {
var tb = $("#tbList");
//先移除旧的,添加新的
$("#tbList tr[type=data]").remove();
for (var i = 0; i < data.Data.length; i++)
{
var strTr = "<tr type='data'>";
strTr += "<td>" + data.Data[i].id + "</td>";
strTr += "<td>" + data.Data[i].realname + "</td>";
strTr += "<td>" + data.Data[i].sex + "</td>";
strTr += "<td><a UId='" + data.Data[i].id + "' href='javascript:void(0)'>修改</a>" +
"<a UId='" + data.Data[i].ID + "' href='javascript:void(0)'>删除</a></td>";
strTr += "</tr>";
tb.append(strTr);
}
$("#Nav").html(data.NavStr); //绑定分页标签的点击事件
$(".pageLink").click(function () {
//把页码弹出来
var strHref = $(this).attr("href");
var queryStr = strHref.substr(strHref.indexOf('?') + 1);
//alert(queryStr);
initTable(queryStr);
return false;
});
});
}
</script>
</head>
<body>
<div>
<table id="tbList">
<tr>
<td>id</td>
<td>姓名</td>
<td>性别</td>
<td>操作</td>
</tr>
</table> <div id="Nav" class="paginator"> </div> </div>
</body>
</html>

  3:分页类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; namespace MvcTest4.Models
{
public class PageNavHelper
{
//主要就是输出分页的超级链接的标签
//自定义分页Helper扩展
public static string ShowPageNavigate(int currentPage, int pageSize, int totalCount)
{
var redirectTo = HttpContext.Current.Request.Url.AbsolutePath;
pageSize = pageSize <= 0 ? 3 : pageSize;
var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
var output = new StringBuilder();
if (totalPages > 1)
{
//if (currentPage != 1)
{//处理首页连接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a> ", redirectTo, pageSize);
}
if (currentPage > 1)
{//处理上一页的连接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a> ", redirectTo, currentPage - 1, pageSize);
}
else
{
// output.Append("<span class='pageLink'>上一页</span>");
} output.Append(" ");
int currint = 5;
for (int i = 0; i <= 10; i++)
{//一共最多显示10个页码,前面5个,后面5个
if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
{
if (currint == i)
{//当前页处理
//output.Append(string.Format("[{0}]", currentPage));
output.AppendFormat("<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage, pageSize, currentPage);
}
else
{//一般页处理
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
}
}
output.Append(" ");
}
if (currentPage < totalPages)
{//处理下一页的链接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a> ", redirectTo, currentPage + 1, pageSize);
}
else
{
//output.Append("<span class='pageLink'>下一页</span>");
}
output.Append(" ");
if (currentPage != totalPages)
{
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a> ", redirectTo, totalPages, pageSize);
}
output.Append(" ");
}
output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行 return output.ToString();
}
}
}

  4:分页CSS

body {
} .paginator {
font: 12px Arial, Helvetica, sans-serif;
padding: 10px 20px 10px 0;
margin: 0px;
} .paginator a {
border: solid 1px #ccc;
color: #0063dc;
cursor: pointer;
text-decoration: none;
} .paginator a:visited {
padding: 1px 6px;
border: solid 1px #ddd;
background: #fff;
text-decoration: none;
} .paginator .cpb {
border: 1px solid #F50;
font-weight: 700;
color: #F50;
background-color: #ffeee5;
} .paginator a:hover {
border: solid 1px #F50;
color: #f60;
text-decoration: none;
} .paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover {
float: left;
height: 16px;
line-height: 16px;
min-width: 10px;
_width: 10px;
margin-right: 5px;
text-align: center;
white-space: nowrap;
font-size: 12px;
font-family: Arial,SimSun;
padding: 0 3px;
}

  

MVC异步分页的更多相关文章

  1. asp.net MVC 异步分页 PagedList

    最近做一个项目要有的异步分页,先记录下来! 引用: PagedList.css MvcPager.js <link href="~/css/sweetalert2.min.css&qu ...

  2. ASP.NET MVC中使用MvcPager异步分页+在分页中复选框下一页上一页也保持选中

    ASP.NET MVC 分页使用的是作者杨涛的MvcPager分页控件  地址:http://www.webdiyer.com/mvcpager/demos/ajaxpaging/ 这个分页控件在里面 ...

  3. Asp.net MVC 使用PagedList(新的已更名 为X.PagedList.Mvc) 分页

    在asp.net mvc 中,可以bootstrap来作为界面,自己来写分页程序.也可以使用PagedList(作者已更名为 X.PagedList.Mvc)来分页. 1.首先,在NuGet程序包管理 ...

  4. 基于存储过程的MVC开源分页控件--LYB.NET.SPPager

    摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件MVCPager(http://www.webdiyer.com/)算 ...

  5. 基于layerpage 前后端异步分页

    #下载jquery 和 layerpage1.核心分页方法 laypage({ cont: 'page1', //容器.值支持id名.原生dom对象,jquery对象. pages: json.tot ...

  6. Spring+Mybatis+jQuery.Pagination.js异步分页及JsonConfig的使用

    在开发工作中经常用到异步分页,这里简单整理一下资料. 一.Controller方法 package com.lwj.controller; import javax.servlet.http.Http ...

  7. jQuery异步分页插件

    学校软件工程让写课程设计(其实就是自选语言做个项目),感觉都是重复的东西就没有很认真的去写内容,更加注意写一些之前没有用过的东西. 因为一直都使用TP框架来写PHP,TP又自带分页类,想到这里就想试试 ...

  8. mvc异步表单遇到的问题

    1,mvc异步表单遇到的问题    问题:使用jqury easy ui 时提交异步数据不能请求到服务器   解决办法:经过细心调试和检测,发现jqury的加载顺序放在了easy ui之后,所以首先加 ...

  9. Mvc自定义分页控件

    MVC开发分页常常使用第三方控件,生成的分页HTML带有版权申明,虽然免费,但是总有的别扭.于是,某日,楼主闲来蛋疼,折腾了个自定义分页控件: 先来展示下效果图: 1>当分页不超过10页的时候, ...

随机推荐

  1. Spring中AOP实现

    1.什么是SpringAOP 什么是aop:Aspect Oriented Programming的缩写,面向切面编程,通过预编译和动态代理实现程序功能的 统一维护的一种技术 主要功能:日志记录,性能 ...

  2. 017 jquery中对样式的操作

    1.样式操作 2.css-dom操作 3.程序 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  3. vuex使用modules namespaced 后,模块名不同,函数名相同时候在组件中分发Action

    你在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用 ...

  4. Spring拦截器和过滤器

    什么是拦截器 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对象,允许开发者 ...

  5. linux学习笔记-5.用户和组

    1.添加一个tom用户,设置它属于users组,并添加注释信息 分步完成: useradd tom usermod -g users tom usermod -c "hr tom" ...

  6. 循序渐进学.Net Core Web Api开发系列【1】:开发环境

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.本篇概述 本篇不 ...

  7. HTML5开启浏览器桌面通知 Web Notification

    说明: 1.Chrome要求必须https才可以开启浏览器通知 2.显示图片在本服务器,不支持跨越 3.自定义声音Chrome不播放,Firefox正常播放 代码如下: <!-- /** * @ ...

  8. Linux 系统及编程相关知识总汇

    Linux  C function() 参考手册 STL 学习文档 Linux内核

  9. 图的基本操作(基于邻接表):图的构造,深搜(DFS),广搜(BFS)

    #include <iostream> #include <string> #include <queue> using namespace std; //表结点 ...

  10. hdu 4431 第37届ACM/ICPC 天津赛区现场赛A题 枚举

    题意:就是给了13张牌.问增加哪些牌可以胡牌.m是数字,s是条,p是筒,c是数字 胡牌有以下几种情况: 1.一个对子 +  4组 3个相同的牌或者顺子.  只有m.s.p是可以构成顺子的.东西南北这样 ...