Enhanced PagedList for ASP.NET MVC
ASP.NET MVC已经发布了新的源代码,但不是官方版本。大家可以到http://codeplex.com/aspnet看看。
在实际项目中,肯定不会像Scottgu的demo那么简单。比如我们需要将数据分页显示。这的确是一个非常重要的功能。
MVCToolkit下一个版本会包含一个PagedList<T>支持数据分页,但我已经等不及了。
下面就是经过改进的分页类。
首先定义一个接口来包含我们需要的功能。
1
/// <summary>
2
/// 分页列表接口
3
/// </summary>
4
public interface IPagedList
5
{
6
int PageSize { get; set; }
7
int PageNumber { get; set; }
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
int TotalCount { get; }
10
int PageCount { get; }
11
bool HasPreviousPage { get; }
12
bool HasNextPage { get; }
13
}
14![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
现在实现PagedList<T>:
1
/// <summary>
2
/// 分页列表泛型类
3
/// </summary>
4
/// <typeparam name="T">集合元素类型</typeparam>
5
public class PagedList<T> : List<T>, IPagedList
6
{
7
/// <summary>
8
/// 利用现有的IQueryable<T>列表创建一个新的分页列表
9
/// </summary>
10
/// <param name="source">现有的元素集合</param>
11
/// <param name="page">页号</param>
12
/// <param name="pageSize">分页大小</param>
13
public PagedList(IQueryable<T> source, int page, int pageSize)
14
{
15
this.TotalCount = source.Count();
16
this.PageNumber = page;
17
this.PageSize = pageSize;
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
if (this.PageCount < page || page <= 0)
20
throw new ArgumentOutOfRangeException("page");
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
this.AddRange(source.Skip((page - 1) * pageSize).Take(pageSize).ToList());
23
}
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
/// <summary>
26
/// 利用现有的列表List<T>创建一个新的分页列表
27
/// </summary>
28
/// <param name="source">现有的元素集合</param>
29
/// <param name="page">页号</param>
30
/// <param name="pageSize">分页大小</param>
31
public PagedList(List<T> source, int page, int pageSize)
32
{
33
this.TotalCount = source.Count();
34
this.PageNumber = page;
35
this.PageSize = pageSize;
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
if (this.PageCount < page || page <= 0)
38
throw new ArgumentOutOfRangeException("page");
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
this.AddRange(source.Skip((page - 1) * pageSize).Take(pageSize).ToList());
41
}
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
/// <summary>
44
/// 获取表示列表元素总个数的整数
45
/// </summary>
46
public int TotalCount { get; private set; }
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
/// <summary>
49
/// 获取或设置一个表示当前页号的整数
50
/// </summary>
51
public int PageNumber { get; set; }
52
53
/// <summary>
54
/// 获取或设置一个整数来表示分页大小
55
/// </summary>
56
public int PageSize { get; set; }
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
/// <summary>
59
/// 获取一个表示总页数的整数
60
/// </summary>
61
public int PageCount
62
{
63
get
64
{
65
if (PageSize == 0)
66
throw new ArgumentOutOfRangeException("page");
67![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
int remainder = TotalCount % PageSize;
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
if (remainder == 0)
71
return TotalCount / PageSize;
72
else
73
return (TotalCount / PageSize) + 1;
74
}
75
}
76![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
/// <summary>
78
/// 获取一个布尔值表示当前页是否有上一页
79
/// </summary>
80
public bool HasPreviousPage
81
{
82
get
83
{
84
return (PageNumber - 1) > 0;
85
}
86
}
87![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
/// <summary>
89
/// 获取一个布尔值表示当前页是否有下一页
90
/// </summary>
91
public bool HasNextPage
92
{
93
get
94
{
95
return (PageNumber * PageSize) < TotalCount;
96
}
97
}
98
}
99![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
再建立一个扩展类,就完成了添加分页功能。建议将这些类放在System.Web.Mvc命名空间下,这样就可以直接在您的项目中使用了:
1
/// <summary>
2
/// 分页列表扩展类
3
/// </summary>
4
public static class Pagination
5
{
6
/// <summary>
7
/// 将IQueryable<T>转换成分页列表PagedList<T>的一个实例
8
/// </summary>
9
/// <typeparam name="T">元素类型</typeparam>
10
/// <param name="source">要转换的集合,这里是LINQ结果集</param>
11
/// <param name="page">当前页号</param>
12
/// <param name="pageSize">分页大小</param>
13
/// <returns></returns>
14
public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int page, int pageSize)
15
{
16
return new PagedList<T>(source, page, pageSize);
17
}
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
/// <summary>
20
/// 使用默认分页大小(pageSize=10)将IQueryable<T>转换成分页列表PagedList<T>的一个实例,
21
/// </summary>
22
/// <typeparam name="T">元素类型</typeparam>
23
/// <param name="source">要转换的集合,这里是LINQ结果集</param>
24
/// <param name="page">当前页号</param>
25
/// <returns></returns>
26
public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int page)
27
{
28
return new PagedList<T>(source, page, 5);
29
}
30
}
31![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
扩展完成。我们现在可以从LINQ数据集里创建PagedList了:
图1:将LINQ数据集转换为分页列表
![](https://www.cnblogs.com/images/cnblogs_com/shinyzhu/pagedlist.jpg)