最近刚刚接触MVC不久,因项目中要用到分页,网上找了下资料,最后采用了MvcPager(http://www.webdiyer.com/),支持同步和Ajax异步分页。废话不多说了直接上代码。
一.MvcPager异步
ViewModel:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
public class Article
{
[Display(Name = "信息编号")]
public int ID { get; set; }
[Display(Name = "信息标题")]
public string Title { get; set; }
[Display(Name = "信息内容")]
public string Content { get; set; }
}
|
?
|
1
2
3
4
|
public class AjaxPager
{
public PagedList<Article> Articles { get; set; }
}
|
Control:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/// <summary>
/// 异步分页测试
/// </summary>
/// <param name="id">pageIndex</param>
/// <param name="key">关键字</param>
/// <returns></returns>
public ActionResult AjaxPaging(int? id = 1, string key = null)
{
int totalCount = 0;
int pageIndex = id ?? 1;
int pageSize = 2;
List<Article> infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - 1) * 2, out totalCount);
PagedList<Article> InfoPager = infoList.AsQueryable().OrderByDescending(o => o.ID).ToPagedList(pageIndex, pageSize);
InfoPager.TotalItemCount = totalCount;
InfoPager.CurrentPageIndex = (int)(id ?? 1);
Models.MyTest.AjaxPager model = new Models.MyTest.AjaxPager();
model.Articles = InfoPager;
if (Request.IsAjaxRequest())
{
return PartialView("_ArticleList", model);
}
return View(model);
}
|
View:
?
|
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
|
@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 = 5,
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>
|
?
|
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
|
@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(此处可不增加,直接和异步的共用同一个):
?
|
1
2
3
4
5
|
public class MVCPager
{
//信息列表
public PagedList<Article> Articles { get; set; }
}
|
Control:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/// <summary>
/// 同步分页测试
/// </summary>
/// <param name="id">pageIndex</param>
/// <param name="key">关键字</param>
/// <returns></returns>
public ActionResult MVCPager(int? id = 1, string key = null)
{
int totalCount = 0;
int pageIndex = id ?? 1;
int pageSize = 2;
List<Article> infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - 1) * 2, out totalCount);
PagedList<Article> InfoPager = infoList.AsQueryable().OrderByDescending(o => o.ID).ToPagedList(pageIndex, pageSize);
InfoPager.TotalItemCount = totalCount;
InfoPager.CurrentPageIndex = (int)(id ?? 1);
//数据组装到viewModel
Models.MyTest.MVCPager model = new Models.MyTest.MVCPager();
model.Articles = InfoPager;
return View(model);
}
|
View:
?
|
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
46
47
48
49
50
51
52
53
54
55
56
|
@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>
|
获取测试数据方法(共用):
?
|
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
|
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 > 0)
{
foreach (DataRow dr in ds.Tables[0].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)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/xiaoxiaocainia/p/5563633.html
相关文章
猜你喜欢
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 102
-
PHP图像处理 imagestring添加图片水印与文字水印操作示例
2025-05-29 36 -
2025-05-27 41
-
2025-05-27 80
-
iOS开发中使用UIScrollView实现图片轮播和点击加载
2025-05-29 22
热门评论


