杨涛老师MvcPager示例
杨涛老师插件地址:http://www.webdiyer.com/mvcpager
杨涛老师网站上示例写的很详细了,参考他的示例和帮助文档就可以运用的很好了,有经验的同学可以直接参考官方示例。
一、标准的ajax分页
1、新建一个空的MVC项目

1 /// <summary
2 /// 单个分页
3 /// </summary>
4 /// <returns></returns>
5 [HttpGet]
6 public ActionResult SinglePage(int id = 1)
7 {
8 List<Article> articles = new List<Article>();
9 for (int i = 0; i < 10; i++)
10 {
11 Article a = new Article();
12 a.ID = id;
13 a.Title = "Title " + id;
14 a.Content = "Content " + id;
15 articles.Add(a);
16 }
//注:看官网问题留言有部分同学在看了杨老师demo之后不清楚如何根据条件去数据库取对应页的数据,而不是将集合数据取出来再进行分页,其实只要把源码下载下来看一下里面方法的实现就好了。
17 PagedList<Article> model = new PagedList<Article>(articles, id, 10, 100);//articles-当前页记录、id-页码、10-每页记录条数、100-总记录条数
18 if (Request.IsAjaxRequest())
19 return PartialView("_ArticleList", model);
20 return View(model);
21 }


1 @using Webdiyer.WebControls.Mvc
2 @model PagedList<MvcPagerTest.Models.Article>
3
4 <div id="articles">
5 @Html.Partial("_ArticleList", Model)
6 </div>
7 @section scripts
8 {
9 @{Html.RegisterMvcPagerScriptResource();}
10 }


1 @using Webdiyer.WebControls.Mvc
2 @model PagedList<MvcPagerTest.Models.Article>
3
4 <div class="text-center">
5 @Ajax.Pager(Model, new PagerOptions { PageIndexParameterName = "id", ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=\"active\"><a href=\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>" }).AjaxOptions(a => a.SetUpdateTargetId("articles"))
6 </div>
7
8 @{ Html.RenderPartial("_ArticleTable"); }
9
10 <div class="text-center">
11 @Ajax.Pager(Model, new PagerOptions { AlwaysShowFirstLastPageNumber = true, PageIndexParameterName = "id", ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=\"active\"><a href=\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>" }).AjaxOptions(a => a.SetUpdateTargetId("articles"))
12 </div>


1 @using Webdiyer.WebControls.Mvc
2 @model PagedList<MvcPagerTest.Models.Article>
3
4 <table class="table table-bordered table-striped">
5 <tr>
6 <th class="nowrap">序号</th>
7 <th>
8 @Html.DisplayNameFor(model => model.Title)
9 </th>
10 <th>
11 @Html.DisplayNameFor(model => model.Content)
12 </th>
13 </tr>
14 @{ int i = 0;}
15 @foreach (var item in Model)
16 {
17 <tr>
18 <td>@(Model.StartItemIndex + i++)</td>
19 <td>
20 @Html.DisplayFor(modelItem => item.Title)
21 </td>
22 <td>
23 @Html.DisplayFor(modelItem => item.Content)
24 </td>
25 </tr>
26 }
27 </table>

二、多个ajax分页
多个ajax分页和单个ajax分页类似,这里要注意的是:
1、不同的分页控件要定义不同页码参数名称(如下:第一个定义 为pageIndex,第二个定义为 id)
2、后台通过自定义参数来区分获取的是哪个分页的数据,这里通过AddRouteValue("param","value")来添加
|
1
2
|
//PageIndexParameterName设置页码参数名称; @Ajax.Pager(Model).Options(o=>o.AddRouteValue("target","blog1"))生成分页链接的路由的值。@Ajax.Pager(Model, new PagerOptions { AlwaysShowFirstLastPageNumber = true, PageIndexParameterName = "id", ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=\"active\"><a href=\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>" }).Options(o => o.AddRouteValue("target", "blog2")).AjaxOptions(a => a.SetUpdateTargetId("blog2s")) |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
/// <summary>/// 多个分页/// </summary>/// <returns></returns>[HttpGet]public ActionResult MultiPage(int pageIndex = 1,int id = 1){ if (Request.IsAjaxRequest()) { string target = Request.QueryString["target"];//通过target来区分获取的是哪个分页数据。 if (target == "blog1") { return PartialView("_Blog1List", new PagedList<Blog1>(GetBlog1s(pageIndex), pageIndex, 10, 100));//页码参数名称为pageIndex } if (target == "blog2") { return PartialView("_Blog2List", new PagedList<Blog1>(GetBlog1s(id), id, 10, 100));//页码参数名称为id } } Blog blog = new Blog(); blog.Blog1s = new PagedList<Blog1>(GetBlog1s(pageIndex), pageIndex,10, 100); blog.Blog2s = new PagedList<Blog2>(GetBlog2s(id), id, 10, 100); return View(blog);}public List<Blog1> GetBlog1s(int pageIndex){ List<Blog1> blog1s = new List<Blog1>(); for (int i = 0; i < 10;i++) { blog1s.Add(new Blog1(pageIndex, "name1-" + pageIndex, "content1-"+ pageIndex)); } return blog1s;}public List<Blog2> GetBlog2s(int id){ List<Blog2> blog2s = new List<Blog2>(); for (int i = 0; i < 10; i++) { blog2s.Add(new Blog2(id, "name2-" + id, "content2-" + id)); } return blog2s;} |
示例地址(vs2015):https://files.cnblogs.com/files/zhaorong0912/MvcPagerDemo.rar
最后:文章旨在记录自己的所学所用,如有错误,希望各位能及时指出,本人不胜感激!
杨涛老师MvcPager示例的更多相关文章
- 跟开涛老师学shiro -- 编码/加密
在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...
- 跟开涛老师学shiro -- 授权
授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permission).角 ...
- 杨校老师课堂之JavaScript右下角广告弹框教程
案例制作思路: 1.先制作界面 添加一个盒子包含一个按钮,使盒子绝对定位在右上角 添加一个大盒子,同理,将盒子居于左下角:其中内部包含一个顶端盒子和底部盒子 顶端盒子因为是属于大盒子内部的存在,所以宽 ...
- C# - 杨涛分页控件AspNetPager
http://www.webdiyer.com/downloads/ 前台 <%@ Page Language="C#" AutoEventWireup="true ...
- 跟开涛老师学shiro -- INI配置
之前章节我们已经接触过一些INI配置规则了,如果大家使用过如spring之类的IoC/DI容器的话,Shiro提供的INI配置也是非常类似的,即可以理解为是一个IoC/DI容器,但是区别在于它从一个根 ...
- 跟开涛老师学shiro -- 身份验证
身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...
- 跟开涛老师学shiro -- shiro简介
1.1 简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Securi ...
- 传智 杨中科老师 ASP.NET 笔记
- ASP.NET MVC中使用MvcPager异步分页+在分页中复选框下一页上一页也保持选中
ASP.NET MVC 分页使用的是作者杨涛的MvcPager分页控件 地址:http://www.webdiyer.com/mvcpager/demos/ajaxpaging/ 这个分页控件在里面 ...
随机推荐
- Android ListView的使用(一)
初次接触listview,以为直接在Android Studio 中将控件给拖过去,就能够使用了,结果半天显示不了. 后来总算知道原因了. 先上代码: activity_main.xml 显示页面 里 ...
- C#使用ICSharpCode.SharpZipLib压缩后进行web批量下载文件
参考:http://blog.csdn.net/kongwei521/article/details/51167903#
- centos 6.4 x86_64 (minimal) 编译安装percona
下载Percona-Server-5.5.24-26.0 wget https://www.percona.com/downloads/Percona-Server-5.5/Percona-Serve ...
- 为什么hash作为内存使用的经典数据结构?
听到这样说法:hash是内存中使用的经典数据结构.内存是典型的随机访问设备. 为什么hash这种数据结构很适合内存使用呢?如何理解内存是随机访问设备呢? 因为我想知其所以然,如何理解背后的原因,我花费 ...
- 简析TCP的三次握手与四次分手<转>
TCP是什么? 具体的关于TCP是什么,我不打算详细的说了:当你看到这篇文章时,我想你也知道TCP的概念了,想要更深入的了解TCP的工作,我们就继续.它只是一个超级麻烦的协议,而它又是互联网的基础,也 ...
- Android——浅谈HTTP中Get与Post的区别(转)
原文地址:http://network.51cto.com/art/201407/446434.htm Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DEL ...
- ES06--elasticsearch
ES06--elasticsearch unassigned错误解决(手动处理) 查看集群健康状态:curl -XGET http://localhost:9200/_cluster/health ...
- 局域网内的机器不能ping通虚拟机,但是虚拟机可以ping通局域网的机器
将虚拟机的网络网络适配器模式改成桥接模式即可: 一.选择虚拟机里面的设置: 二.在虚拟机设置界面将网络适配器模式改成桥接模式
- Vue 获取自定义属性的值
在jquery中,如果要获取 data-*** 的值可以通过 $('目标元素').data('属性名') 来获取. 在Vue中如何获取该值呢? 1.换个思路,作为参数传递. 如下代码: <but ...
- sparkr脚本
test <- function(){ print(1) } test() 脚本内为单独函数时 调用脚本不执行 所以要在脚本内调取特定函数