在ASP.NET中,GridView控件本身就带有分页的功能,只要把当前页面的索引值赋给GridView的PageIndex就可以实现了分页,至于数据怎么分页,全都由GridView封装起来了。
在ASP.NET MVC中分页的实现就只能靠自己来设计了。首先来说一下分页的原理。现在有一个张news表,中间有很多信息,我们假设一页显示5条记录,这样,页面数量=总记录数/5;这里,如果出现小数,得进位取整。如比总记录和数为51,那么分的页面数量是11,最后一页只有一条记录。好,有这样一个思路后,现在就来做ASP.NET MVC中的分页了。
1、 数据库和表
创建一个Data_Company的数据库,建一张news表,表结构如下图:
![clip_image001 clip_image001](https://images.cnblogs.com/cnblogs_com/axzxs2001/WindowsLiveWriter/ASP.NETMVP_10A13/clip_image001_thumb.jpg)
ID是主键,并助是自动增长列。
2、 现在,我们用O/R Designer来创建news表的LINQ To SQL的实体类。
创建一个名为MvcCompany的ASP.NET MVC Web Application项目,然后选中Models,右键,“添加”,“新建项”,选中C#中的“数据”,如下图:
![clip_image003 clip_image003](https://images.cnblogs.com/cnblogs_com/axzxs2001/WindowsLiveWriter/ASP.NETMVP_10A13/clip_image003_thumb.jpg)
选择“LINQ to SQL类”,名称设为“CompanyData.dbml”,然后“添加”。
打开“服务器资源管理器”,创建连接,展开表,找到news表,拖到CompanyData.dbml的左边表视图区(注:.dbml视图左边是SQL表和SQL视图区,右边是SQL存储过程和SQL函数区),效果如下图:
![clip_image004 clip_image004](https://images.cnblogs.com/cnblogs_com/axzxs2001/WindowsLiveWriter/ASP.NETMVP_10A13/clip_image004_thumb.jpg)
在“解决方案资源管理器”下的,“Models”多了三个文件,CompanyData.dbml,展开它,会有CompanyData.dbml.layout和CompanyData.designer.cs,前者是CompanyData.dbml视图的一些信息(比如news表在的坐标等信息),后者是news实体类及Data_Company数据库的类,在数据库类中聚合了news实体类。
3、 添加news列表类。因为一个news实体类,一次只能表示一条记录,如果呈现一个news表中的数据,最好定义一个集合类来存临时来存放news记录的集合。
类的实现如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
using System.Collections.Generic;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
using System.Linq;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
using System.Web;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
namespace MvcCompany.Models
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
public class NewList<T> : List<T>
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
/// 页面索引值
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
/// </summary>
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int PageIndex
{ get; private set; }
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
/// 每页记录的数量
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
/// </summary>
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int PageSize
{ get; private set; }
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
/// 记录总条数
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
/// </summary>
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int TotalCount
{ get; private set; }
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
/// 共有的页数和
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
/// </summary>
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int TotalPages
{ get; private set; }
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
public NewList(IQueryable<T> source, int pageIndex, int pageSize)
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
PageIndex = pageIndex;
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
PageSize = pageSize;
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
TotalCount = source.Count();
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
// 进上去取整( 总记录条数/一面记录的条数)
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
TotalPages = (int)Math.Ceiling(TotalCount / (double)pageSize);
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
this.AddRange(source.Skip(pageIndex * pageSize).Take(PageSize));
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
}
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
/// 是否存在前续页
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
/// </summary>
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
public bool HasPreviousPage
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
76![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return (PageIndex > 0); }
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
}
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
82![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
/// 是否存在后续页
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
/// </summary>
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
public bool HasNextPage
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return (PageIndex + 1 < AllPages); }
92![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
}
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
}
96![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
}
98![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
99![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这里,我们实现了一个泛型的集合列表NewList(当然,这里的本质上可以当其他实体类的集合列表),在这个类中间,有四个字段,访问修饰符都是public的,分另为:
PageIndex:当前页面的索引值
PageSize:每个页面的记录的条数
AllCount:记录的总条数
AllPages:共有的页面总数
在NewList 构造函数中,有如下代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public NewList(IQueryable<T> list, int pageIndex, int pageSize)
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
PageIndex = pageIndex;
6
PageSize = pageSize;
7![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
AllCount = source.Count();
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
AllPages = (int)Math.Ceiling(AllCount / (double)pageSize);
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
this.AddRange(list.Skip(PageIndex * PageSize).Take(PageSize));
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
构造函的参数有三个,一个是list,就是实体类的一个集合,还有就是页面索引值和每个页面的记录条数。
第1、2代码很容易理角,第3行代码是得到列表的总记录条数,第4行代码,就完成了我们在开始时分析的分页实现的公式:页面数量=总记录数/每页记录数,其中Math.Ceiling就是把小数部分进到整数的函数。
最关键的是第5行代码,首先看Skip(PageIndex*PageSize),页面索引值乘上每页记录数,得到是当前页面以前的所有记录数,Skip是跳过这些记录,而得到后面的所有记录,Take(PageSize)是得到PageSize条数的记录,比如,我们想要第三页的记录,这个页面的索引值为2(因为索引值从0开始)PageIndex=2,每页显示5条记录,PageSize=5,就是要跳过list中的前10条记录,然后再取前5条记录,即取list中的第11条到第15条记录,也就是第3页的记录了。
在这里,微软提供了Skip和Tabke函数,让我们做起分页来,得心应手。
接下来是this.AddRange()函数,可以把批量的数据放到当前集合中(因为NewList本身就是一个集合)。
代码的后半部分是两个属性:
public bool HasPreviousPage
{
get { return (PageIndex > 0); }
}
public bool HasNextPage
{
get { return (PageIndex + 1 < AllPages); }
}
这两个属性是为页面显示“上一页”和“下一页”,因为当我们显示第一页的时候,“上一页”是不需要显示的,如果最后一页,是没有“下一页“的,所以在这里定义了两个属性,来判断是否有上一页和下一页。
先看HasPreviousPage属性,如果PageIndex是大于0的,说明不是在第一页,所以就反回true,如果小于等于0(在这里小于0是没有意思义的,因为页面的索引值最小是0),说明是第一页,所以返回是false。
再看HasNextPage属性,因为页面索引值的最大数,与页面的最大数差1(索引从0开始的原因),所以当PageIndex+1小于AllPages时,说没有没到最后一页,返回值是true,如果PageIndex+1大于等于AllPages时(大于也没有意义),说明是最后一页,返回值为false。
4、 添加NewsController。选中Controller,右键添加一个NewsController 的Controller。
代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
using System.Collections.Generic;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
using System.Linq;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
using System.Web;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
using System.Web.Mvc;
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
using System.Web.Mvc.Ajax;
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
using MvcCompany.Models;
14![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
using System.Configuration;
16![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
namespace MvcCompany.Controllers
18![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
public class NewsController : Controller
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
DataClassesDataContext DCDC;
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
int pageSize;
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
public NewsController()
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
DCDC = new DataClassesDataContext();
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pagesize"]); //每个页面的数量存放在web.config的appsetting里的pagesize节中,值为5
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
}
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
public ActionResult Index(int? page)
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
var NewsList = DCDC.news.Select(newss=>newss);
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
var paginatedNews = new NewList<news>(NewsList, page ?? 0, pageSize); //实现分页功能
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
return View(paginatedNews);
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
[AcceptVerbs(HttpVerbs.Post)]
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
public ActionResult Index(FormCollection formValues)
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
int? index = int.Parse(formValues.GetValue("pageindex").AttemptedValue);
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
int page = index ??0 ;
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
var NewsList = DCDC.news.Select(newss => newss);
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
var paginatedNews = new NewList<news>(NewsList, page, pageSize); //实现分页功能
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
return View(paginatedNews);
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
}
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
}
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
}
72![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
73![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
关于两个Index重载,我们在设计完Views再讨论。
5、 添加View。选中NewsController,右键,添加View,会弹出如下图:
![clip_image006 clip_image006](https://images.cnblogs.com/cnblogs_com/axzxs2001/WindowsLiveWriter/ASP.NETMVP_10A13/clip_image006_thumb.jpg)
在这里,我们选中MvcCompany.Models.news,在View content下拉列表中选择List(因为要实现列表分页)。
Index.aspx页面代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcCompany.Models.NewList<MvcCompany.Models.news>>" %>
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
新闻
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
</asp:Content>
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
<!--前半部分-->
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
using (Html.BeginForm())
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
{ %>
16![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
<h2>
18![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
新闻列表</h2>
20![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
<table>
22![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
<tr>
24![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
<th>
26![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
编号
28![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
</th>
30![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
<th>
32![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
题目
34![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
</th>
36![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
<th>
38![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
时间
40![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
</th>
42![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
<th>
44![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
内容
46![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
</th>
48![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
49
</tr>
50![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
foreach (var item in Model)
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
{ %>
54![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
<tr>
56![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
57
<td>
58![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
59
<%= Html.Encode(item.ID)%>
60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
</td>
62![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
63
<td>
64![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
<%= Html.Encode(item.title)%>
66![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
</td>
68![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69
<td>
70![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
71
<%= Html.Encode(String.Format("{0:g}", item.datetimes))%>
72![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
73
</td>
74![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
75
<td>
76![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
77
<%= Html.Encode(item.contents)%>
78![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
79
</td>
80![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
81
</tr>
82![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
} %>
84![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
85
</table>
86![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
87
<hr />
88![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
89
<!--后半部分-->
90![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
=Html.RouteLink("首页", "UpcomingNews", new { page = 0 })%>|
92![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
93![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
if (Model.HasPreviousPage)
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
{%>
96![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
97![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
=Html.RouteLink("上一页", "UpcomingNews", new { page = (Model.PageIndex - 1) })%>|
98![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
} %>
100![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
101![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
if (Model.HasNextPage)
102![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
{%>
104![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
=Html.RouteLink("下一页", "UpcomingNews", new { page = (Model.PageIndex + 1) })%>|
106![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
107![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
} %>
108![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
=Html.RouteLink("尾页", "UpcomingNews", new { page = Model.PageSize - 1 })%>
110![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
111
第
112![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
113
<%=Html.TextBox("pageindex", Model.PageIndex, new {style="width:30px;"})%>页<input
114![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
115
type="submit" value="转到" id="sub" />
116![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
117![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
} %>
118![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
119
</asp:Content>
120![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
121![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
代码的前半部分,很好理解。
代码的后半部分,我们来分析一下,首页,是采用
Html.RouteLink(“首页”,”UpcomingNews”,new{ page=0}),是要定位到名称为”UpcomingNews”的路由,这个路由在Global.asax中,代码如下:
routes.MapRoute(
"UpcomingNews",
"News/page/{page}",
new { controller = "News", action = "Index" });
路由的名称是”UpcominNews”,Url格式是News/page/{page},News和page是url的一种格式,后面{page}是参数,这个参数的名称要与View中,后半部分的new{page=0}的page,以及NewsController中的方法Index(int? page)中的page都必需统一。如果有请求
News/page/2的url,实际上请求的是 news?page=2的页网(路由在ASP.NET MVC官上有说明)。
首页为中的page=0,最终会成为NesController中Index(int? page)方法,的实参,返回首页数据(及数据库表的前5条数据)。
上一页,在当前页数PageIndex的基础上减1。
下一页,在当前页数PageIndex的基础上加1
(注:上面的加减1都不会越界,因为当显示第一页时,“上一页”的超连接就不会显示。当最后一页时,“下一页”的超连接就不会显示)
尾页同首页。
现在的一个问题是“转到”第几页的实现,我们知道,转到第几页的“几”,得用户输入,所以这里用一个Html.TextBox来实现,完全代码是
<%=Html.TextBox("pageindex", Model.PageIndex, new {style="width:30px;"})%>
最终会生成一个名称为pageindex的intup,类型为Text,它的值始终为当前的索引页值Model.PageIndex,其中有一个宽度的属性new {style="width:30px;"}。
同时还有一个<input type="submit" value="转到" id="sub" />用来向后参提交。因为代码中有<form>表单,所以可以在NewsController中接收这个提交。
Index(FormCollection formValues)方法用来处理这个提交。这里会从formValues.GetValue("pageindex").AttemptedValue中午到客户端用户输入“pageindex”文本框中的值。从而来完成定向。
至此,我们就用ASP.NET MVC实现了一个分页功能,相比之下要难ASP.NET的GridView自动分页好多。
在这个例子中,我们在Models层中提供了实体类及news表的集合类。在Controller层增加了NewsController类。在View层增加了News的view,从M-V-C角度,分别完成了对分页的功能。