MVC分页之MvcPager使用
最近刚刚接触MVC不久,因项目中要用到分页,网上找了下资料,最后采用了MvcPager(http://www.webdiyer.com/),支持同步和Ajax异步分页。废话不多说了直接上代码。
一.MvcPager异步
ViewModel:
public class Article
{
[Display(Name = "信息编号")]
public int ID { get; set; } [Display(Name = "信息标题")]
public string Title { get; set; } [Display(Name = "信息内容")]
public string Content { get; set; }
}
public class AjaxPager
{
public PagedList<Article> Articles { get; set; }
}
Control:
/// <summary>
/// 异步分页测试
/// </summary>
/// <param name="id">pageIndex</param>
/// <param name="key">关键字</param>
/// <returns></returns>
public ActionResult AjaxPaging(int? id = , string key = null)
{
int totalCount = ;
int pageIndex = id ?? ;
int pageSize = ;
List<Article> infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - ) * , out totalCount);
PagedList<Article> InfoPager = infoList.AsQueryable().OrderByDescending(o => o.ID).ToPagedList(pageIndex, pageSize);
InfoPager.TotalItemCount = totalCount;
InfoPager.CurrentPageIndex = (int)(id ?? ); Models.MyTest.AjaxPager model = new Models.MyTest.AjaxPager();
model.Articles = InfoPager;
if (Request.IsAjaxRequest())
{
return PartialView("_ArticleList", model);
}
return View(model);
}
View:
@model soulefu_manage.Models.MyTest.AjaxPager
@using Webdiyer.WebControls.Mvc; <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MVCPager-AjaxPaging</title>
<link href="~/Content/pagerstyles.css" rel="stylesheet" />
<link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div style="padding: 15px;">
@using (Html.BeginForm("AjaxPaging", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get))
{
@Html.Label("关键字:") <input name="key" value="@Request.QueryString["key"]" /><input type="submit" value="查询" />
} @*分页Table*@
@{ Html.RenderPartial("_ArticleTable"); } <div class="text-center">
@Ajax.Pager(Model.Articles, new PagerOptions
{
PageIndexParameterName = "id",
FirstPageText = "首页",
PrevPageText = "上一页",
NextPageText = "下一页",
LastPageText = "末页",
NumericPagerItemCount = ,
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"))
</div>
</div>
</body>
</html>
@model soulefu_manage.Models.MyTest.AjaxPager <table class="table table-bordered table-striped">
<tr>
<th class="nowrap">序号</th>
<th>
标题
</th>
<th>
内容
</th>
</tr>
@foreach (var item in Model.Articles)
{
<tr>
<td>@Html.DisplayFor(model => item.ID)</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Content)
</td>
</tr>
}
</table>
二.MvcPager同步
ViewModel(此处可不增加,直接和异步的共用同一个):
public class MVCPager
{
//信息列表
public PagedList<Article> Articles { get; set; }
}
Control:
/// <summary>
/// 同步分页测试
/// </summary>
/// <param name="id">pageIndex</param>
/// <param name="key">关键字</param>
/// <returns></returns>
public ActionResult MVCPager(int? id = , string key = null)
{
int totalCount = ;
int pageIndex = id ?? ;
int pageSize = ;
List<Article> infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - ) * , out totalCount);
PagedList<Article> InfoPager = infoList.AsQueryable().OrderByDescending(o => o.ID).ToPagedList(pageIndex, pageSize);
InfoPager.TotalItemCount = totalCount;
InfoPager.CurrentPageIndex = (int)(id ?? ); //数据组装到viewModel
Models.MyTest.MVCPager model = new Models.MyTest.MVCPager();
model.Articles = InfoPager;
return View(model);
}
View:
@model soulefu_manage.Models.MyTest.MVCPager
@using Webdiyer.WebControls.Mvc; <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MVCPager</title>
<link href="~/Content/pagerstyles.css" rel="stylesheet" />
<link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div style="padding:15px;">
@using (Html.BeginForm("MVCPager", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get))
{
@Html.Label("关键字:")<input name="key" value="@Request.QueryString["key"]" /><input type="submit" value="查询" />
} <table class="table table-bordered table-striped">
<tr>
<th>编号</th>
<th>标题</th>
<th>内容</th>
</tr>
@foreach (var info in Model.Articles)
{
<tr>
<td>@Html.DisplayFor(model => info.ID)</td>
<td>@Html.DisplayFor(model => info.Title)</td>
<td>@Html.DisplayFor(model => info.Content)</td>
</tr>
}
</table> <div class="text-center">
<nav>
@Html.Pager(Model.Articles, new PagerOptions
{
PageIndexParameterName = "id",
FirstPageText = "首页",
PrevPageText = "上一页",
NextPageText = "下一页",
LastPageText = "末页",
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>",
Id = "bootstrappager"
})
</nav>
</div>
</div>
</body>
</html>
获取测试数据方法(共用):
public class MyTest
{
/// <summary>
/// 获取测试数据
/// </summary>
/// <param name="key"></param>
/// <param name="PageSize"></param>
/// <param name="CurrentCount"></param>
/// <param name="TotalCount"></param>
/// <returns></returns>
public List<Article> GetArticleList(string key, int PageSize, int CurrentCount, out int TotalCount)
{
string tabName = string.Format("Article");
string strWhere = " 1=1";
if (!string.IsNullOrEmpty(key))
{
//SQL关键字过滤 包含关键字则不拼接SQL
if (!SqlInjection.GetString(key))
{
strWhere += string.Format(" AND (Title LIKE '%{0}%' OR Content LIKE '%{0}%')", key);
}
}
string Order = string.Format("ID ASC");
DataSet ds = SqlHelper.GetList(SqlHelper.connStr, Order, PageSize, CurrentCount, tabName, strWhere, out TotalCount);
List<Article> list = new List<Article>();
if (ds != null && ds.Tables.Count > )
{
foreach (DataRow dr in ds.Tables[].Rows)
{
Article model = new Article();
model.ID = Convert.ToInt32(dr["ID"]);
model.Title = dr["Title"].ToString();
model.Content = dr["Content"].ToString();
list.Add(model);
}
}
return list;
}
}
效果图:(需要引用CSS)

注意:需要引用MvcPager.DLL文件
MVC分页之MvcPager使用的更多相关文章
- ASP.NET MVC分页组件MvcPager 2.0版发布暨网站全新改版
MvcPager分页控件是在ASP.NET MVC Web应用程序中实现分页功能的一系列扩展方法,该分页控件的最初的实现方法借鉴了网上流行的部分源代码, 尤其是ScottGu的PagedList< ...
- ASP.NET MVC中使用MvcPager异步分页+在分页中复选框下一页上一页也保持选中
ASP.NET MVC 分页使用的是作者杨涛的MvcPager分页控件 地址:http://www.webdiyer.com/mvcpager/demos/ajaxpaging/ 这个分页控件在里面 ...
- MVC分页
http://www.cnblogs.com/iamlilinfeng/p/4075292.html 目录 一.Contrl与View数据传递(多表数据) 二.分页控件介绍 三.MVC源码说明 四.源 ...
- 转:MVC分页
原文地址:http://www.cnblogs.com/iamlilinfeng/p/4075292.html 分页总是搞得我很烦,也是因为刚接触,貌似有好多插件,之前在用一个,可是后来发现一翻页原来 ...
- ASP.NET MVC 4使用PagedList.Mvc分页
ASP.NET MVC中进行分页的方式有多种,在NuGet上有提供使用PagedList.PagedList.Mvc进行分页. 1. 通过NuGet引用PagedList.Mvc 在安装引用Paged ...
- ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了 ...
- Mvc 分页栏扩展方法
using System; using System.Collections.Generic; using System.Reflection; using System.Text; using Sy ...
- spring mvc 分页
spring mvc 分页
- 基于Bootstrap的Asp.net Mvc 分页
基于Bootstrap的Asp.net Mvc 分页的实现 最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一 ...
随机推荐
- 【jquery】基于 jquery 实现 ie 浏览器兼容 placeholder 效果
placeholder 是 html5 新增加的属性,主要提供一种提示(hint),用于描述输入域所期待的值.该提示会在输入字段为空时显示,并会在字段获得焦点时消失.placeholder 属性适用于 ...
- chrome https添加信任
在浏览器地址栏输入:chrome://net-internals/#hsts 然后到Add domain下,Domain添上诸如google.com和google.com.hk ,并勾选Include ...
- UDP网络通信OSC 协议
使用方法 ofxOscMessage mesg; mesg.setAddress("m"); mesg.addIntArg(); mesg.addIntArg(); mesg.ad ...
- SSAS:OLE DB 错误: OLE DB 或 ODBC 错误 : Login failed for user 'NT Service\MSSQLServerOLAPService'
问题描述 按照微软官方教程尝试使用SSAS做OLAP时,出现如下错误信息: Severity Code Description Project File Line Suppression State ...
- seajs集成jquery的一个坑
var $ = require("jquery"); 今天在用seajs集成js的时候,老是发现$获取不到,但是文件又加载进去了,后来找了半天发现是这个问题. 本质的原因在于sea ...
- Android之TextView密码输入变星号时间
private static class Visible extends Handler implements UpdateLayout, Runnable{ public Visible(Spann ...
- 内容营销三大实用法则(内含干货)-同样可运用在EDM数据营销中
内容为王的时代,注重内容的发展才能屹立于互联网的浪潮之中.一个优秀内容在搜寻引擎优化.用户互动,促进销售等方面都扮演重要的角色,博主在这方面深有体会,但是很多人往往走向事情的反面,不注重实际的内容,而 ...
- 那些年我们赚过的外快(POS(移动支付)接口开发)
老规矩上前戏了.在我写博文"那些年我们赚过的外快"前后算起来大大小小也接了些私活,这次是因为好久没写博客了,趁热分享一下.最近回了离老家近的二线城市成都工作,收入那是下降很多啊,刚 ...
- Maximum Entropy Markov Models for Information Extraction and Segmentation
1.The use of state-observation transition functions rather than the separate transition and observat ...
- netty 学习
示例 : wikit http://netty.io/wiki/index.html 书 : netty in action http://blog.csdn.net/abc_key/article/ ...