看了老赵的MVC课程(27):辅助普通Web应用程序开发(三)——Model Binder,编译不通过,稍加了修改
在webcast里实在找不出这一讲的源代码,无奈之下,自己动手稍作了一下修改.这样能编译,也能按规矩运行了.源代码下载:PartialViewTest.rar
先来看一下文档的位置,我比较懒,直接帖图比较方便:
![](https://images.cnblogs.com/cnblogs_com/corecainiao/未标题-1.gif)
然后,我把自己的代码帖出来,不管这么多了,从上往下帖:
首先是HomeController.cs,因为我就是用这个HomeController来做的.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Web;
5
using System.Web.Mvc;
6
using PartialViewTest.Views.Home;
7
using PartialViewTest.Models;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
namespace PartialViewTest.Controllers
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
11
[HandleError]
12
public class HomeController : Controller
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
public ActionResult Index()
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
List<UserInfo> userInfo = new List<UserInfo>()
{
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 0,UserName = "张一",Age = 20, Sex = "男",Email = "aklsdf@163.com"},
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 1,UserName = "李二",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 2,UserName = "李三",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 3,UserName = "张四",Age = 20, Sex = "男",Email = "aklsdf@163.com"},
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 4,UserName = "李五",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 5,UserName = "李六",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 6,UserName = "张七",Age = 20, Sex = "男",Email = "aklsdf@163.com"},
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 7,UserName = "李八",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 8,UserName = "李九",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 9,UserName = "张十",Age = 20, Sex = "男",Email = "aklsdf@163.com"}
27
};
28
return View(new IndexModel(userInfo,6,2,100));
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
public ActionResult List(int page)
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
List<UserInfo> userInfo = new List<UserInfo>()
{
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = page,UserName = "张一",Age = 20, Sex = "男",Email = "aklsdf@163.com"},
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 1,UserName = "李二",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 2,UserName = "李三",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 3,UserName = "张四",Age = 20, Sex = "男",Email = "aklsdf@163.com"},
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 4,UserName = "李五",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 5,UserName = "李六",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 6,UserName = "张七",Age = 20, Sex = "男",Email = "aklsdf@163.com"},
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 7,UserName = "李八",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 8,UserName = "李九",Age = 21, Sex = "男",Email = "aklsdf@163.com"},
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new UserInfo
{UserId = 9,UserName = "张十",Age = 20, Sex = "男",Email = "aklsdf@163.com"}
44
};
45
//ViewData["Message"] = "Welcome to ASP.NET MVC!";
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
return View(new IndexModel(userInfo, 6, page, 100));
48
}
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
public ActionResult About()
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
return View();
53
}
54
}
55
}
56![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
接下来,是我的Models里的一个用户类UserInfo.cs:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Linq;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12
13 namespace PartialViewTest.Models
14 {
15 public class UserInfo
16 {
17 public int UserId
18 {
19 set;
20 get;
21 }
22 public string UserName
23 {
24 set;
25 get;
26 }
27
28 public string Sex
29 {
30 set;
31 get;
32 }
33
34 public int Age
35 {
36 set;
37 get;
38 }
39
40 public string Email
41 {
42 set;
43 get;
44 }
45 }
46 }
47
接下来,是几个View的文件,包括那些在View里的Model文件:
Index.aspx:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PartialViewTest.Views.Home.IndexModel>" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<table width="500" border="0" cellpadding="0" cellspacing="0">
<%Html.RenderPartial("IndexUserRow",new PartialViewTest.Views.Home.IndexUserRowModel(true,null,0));%>
<%((List<PartialViewTest.Models.UserInfo>)Model.UserInfo).ForEach((u) => Html.RenderPartial("IndexUserRow", new PartialViewTest.Views.Home.IndexUserRowModel(false, u, 0))); %>
</table>
<p>
<%Html.RenderPartial("Pager", new PartialViewTest.Views.Shared.PagerModel(Model.PageSize, Model.PageNumber, Model.TotalCount));%>
</p>
</asp:Content>
IndexModel.cs:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PartialViewTest.Models;
namespace PartialViewTest.Views.Home
{
public class IndexModel
{
public IndexModel(IEnumerable<UserInfo> userInfo, int pageSize, int pageNumber, int totalCount)
{
this.UserInfo = userInfo;
this.PageSize = pageSize;
this.PageNumber = pageNumber;
this.TotalCount = totalCount;
}
public IEnumerable<UserInfo> UserInfo
{
get;
private set;
}
public int PageSize
{
get;
private set;
}
public int PageNumber
{
get;
private set;
}
public int TotalCount
{
get;
private set;
}
}
}
唉呀,真多,幸好这只是一个测试的东西.
IndexUserRow.ascx:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PartialViewTest.Views.Home.IndexUserRowModel>" %>
<%PartialViewTest.Views.Heplers.If(Model.RenderHeader,
() =>
{%>
<tr>
<th>
UserId
</th>
<th>
UserName
</th>
<th>
UserSex
</th>
<th>
Age
</th>
<th>
Email
</th>
</tr>
<%},
() =>
{
string c = (Model.Index % 2 == 0) ? "even" : "odd";%>
<tr class="<%=c%>">
<td><%=Model.UserInfo.UserId%></td>
<td><%=Model.UserInfo.UserName%></td>
<td><%=Model.UserInfo.Sex%></td>
<td><%=Model.UserInfo.Age%></td>
<td><%=Model.UserInfo.Email%></td>
</tr>
<%}
);
%>
然后是它的Model
IndexUserRowModel.cs:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PartialViewTest.Models;
namespace PartialViewTest.Views.Home
{
public class IndexUserRowModel
{
public IndexUserRowModel(bool renderHeader, UserInfo userInfo, int index)
{
this.RenderHeader = renderHeader;
this.UserInfo = userInfo;
this.Index = index;
}
public bool RenderHeader
{
get;
private set;
}
public UserInfo UserInfo
{
get;
private set;
}
public int Index
{
get;
private set;
}
}
}
List.aspx:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PartialViewTest.Views.Home.IndexModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
List
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<table width="500" border="0" cellpadding="0" cellspacing="0">
<%Html.RenderPartial("IndexUserRow",new PartialViewTest.Views.Home.IndexUserRowModel(true,null,0));%>
<%((List<PartialViewTest.Models.UserInfo>)Model.UserInfo).ForEach((u) => Html.RenderPartial("IndexUserRow", new PartialViewTest.Views.Home.IndexUserRowModel(false, u, Model.PageNumber))); %>
</table>
<p>
<%Html.RenderPartial("Pager", new PartialViewTest.Views.Shared.PagerModel(Model.PageSize, Model.PageNumber, Model.TotalCount));%>
</p>
</asp:Content>
Pager.ascx:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PartialViewTest.Views.Shared.PagerModel>" %>
<%= PartialViewTest.Views.Heplers.If(
Model.HasPreviousPage,
() => Html.ActionLink("上一页", "List", new { page = Model.PageNumber - 1 }),
() => "上一页"
)%>
|
<% for (int i = 1; i <= (Model.TotalCount - 1) / Model.PageSize + 1; i++)
{%>
<% if (i > 1) { %> | <%} %>
<% if (i == Model.PageNumber)
{ %>
<strong><%=i%></strong>
<%}
else
{ %>
<%=Html.ActionLink(i.ToString(), "List", new { page=i})%>
<%} %>
<%} %>
|
<%= PartialViewTest.Views.Heplers.If(
Model.HasNextPage,
() => Html.ActionLink("下一页", "List", new { page = Model.PageNumber + 1 }),
() => "下一页"
)%>
PagerModel.cs:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PartialViewTest.Models;
using System.Collections.Generic;
namespace PartialViewTest.Views.Shared
{
public class PagerModel
{
public PagerModel(int pageSize, int pageNumber, int totalCount)
{
this.HasPreviousPage = pageNumber > 1 ? true : false;
this.HasNextPage = pageNumber * pageSize < totalCount ? true : false;
this.MaxPageNumber = (totalCount - 1) / pageSize + 1;
this.PageSize = pageSize;
this.PageNumber = pageNumber;
this.TotalCount = totalCount;
}
public bool HasPreviousPage
{
get;
private set;
}
public bool HasNextPage
{
get;
private set;
}
public int MaxPageNumber
{
get;
private set;
}
public int PageSize
{
get;
private set;
}
public int PageNumber
{
get;
private set;
}
public int TotalCount
{
get;
private set;
}
}
}
还有一个辅助方法Helper.cs:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace PartialViewTest.Views
{
public static class Heplers
{
public static string If(bool condition,Func<string> value,Func<string> elseValue )
{
return condition ? value() : elseValue();
}
public static void ForEach(int includeMin, int excludeMax, Action<int> action)
{
for (int i = includeMin; i < excludeMax; i++) action(i);
}
public static void If(bool condition, Action action, Action elseAction)
{
if (condition)
action();
else if (elseAction != null)
elseAction();
}
public static void ForEach<T>(this IEnumerable<T> items,Action<T> action)
{
foreach (var item in items) action(item);
}
public static void ForEach<T>(this IEnumerable<T> items, Action<T,int> action)
{
int index = 0;
foreach (var item in items) action(item,index++);
}
}
}
最后,我还修改了CSS:
在Site.css最后加了
.even
{
color:#000;
}
.odd
{
color:#f00;
}
好了,以上是我全部代码,不正确的地方,请指正.
先来看一下文档的位置,我比较懒,直接帖图比较方便:
![](https://images.cnblogs.com/cnblogs_com/corecainiao/未标题-1.gif)
然后,我把自己的代码帖出来,不管这么多了,从上往下帖:
首先是HomeController.cs,因为我就是用这个HomeController来做的.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.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/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
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)
![](https://www.cnblogs.com/Images/dot.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.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/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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)
![](https://www.cnblogs.com/Images/dot.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
接下来,是我的Models里的一个用户类UserInfo.cs:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Linq;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12
13 namespace PartialViewTest.Models
14 {
15 public class UserInfo
16 {
17 public int UserId
18 {
19 set;
20 get;
21 }
22 public string UserName
23 {
24 set;
25 get;
26 }
27
28 public string Sex
29 {
30 set;
31 get;
32 }
33
34 public int Age
35 {
36 set;
37 get;
38 }
39
40 public string Email
41 {
42 set;
43 get;
44 }
45 }
46 }
47
接下来,是几个View的文件,包括那些在View里的Model文件:
Index.aspx:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PartialViewTest.Views.Home.IndexModel>" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<table width="500" border="0" cellpadding="0" cellspacing="0">
<%Html.RenderPartial("IndexUserRow",new PartialViewTest.Views.Home.IndexUserRowModel(true,null,0));%>
<%((List<PartialViewTest.Models.UserInfo>)Model.UserInfo).ForEach((u) => Html.RenderPartial("IndexUserRow", new PartialViewTest.Views.Home.IndexUserRowModel(false, u, 0))); %>
</table>
<p>
<%Html.RenderPartial("Pager", new PartialViewTest.Views.Shared.PagerModel(Model.PageSize, Model.PageNumber, Model.TotalCount));%>
</p>
</asp:Content>
IndexModel.cs:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PartialViewTest.Models;
namespace PartialViewTest.Views.Home
{
public class IndexModel
{
public IndexModel(IEnumerable<UserInfo> userInfo, int pageSize, int pageNumber, int totalCount)
{
this.UserInfo = userInfo;
this.PageSize = pageSize;
this.PageNumber = pageNumber;
this.TotalCount = totalCount;
}
public IEnumerable<UserInfo> UserInfo
{
get;
private set;
}
public int PageSize
{
get;
private set;
}
public int PageNumber
{
get;
private set;
}
public int TotalCount
{
get;
private set;
}
}
}
唉呀,真多,幸好这只是一个测试的东西.
IndexUserRow.ascx:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PartialViewTest.Views.Home.IndexUserRowModel>" %>
<%PartialViewTest.Views.Heplers.If(Model.RenderHeader,
() =>
{%>
<tr>
<th>
UserId
</th>
<th>
UserName
</th>
<th>
UserSex
</th>
<th>
Age
</th>
<th>
</th>
</tr>
<%},
() =>
{
string c = (Model.Index % 2 == 0) ? "even" : "odd";%>
<tr class="<%=c%>">
<td><%=Model.UserInfo.UserId%></td>
<td><%=Model.UserInfo.UserName%></td>
<td><%=Model.UserInfo.Sex%></td>
<td><%=Model.UserInfo.Age%></td>
<td><%=Model.UserInfo.Email%></td>
</tr>
<%}
);
%>
IndexUserRowModel.cs:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PartialViewTest.Models;
namespace PartialViewTest.Views.Home
{
public class IndexUserRowModel
{
public IndexUserRowModel(bool renderHeader, UserInfo userInfo, int index)
{
this.RenderHeader = renderHeader;
this.UserInfo = userInfo;
this.Index = index;
}
public bool RenderHeader
{
get;
private set;
}
public UserInfo UserInfo
{
get;
private set;
}
public int Index
{
get;
private set;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PartialViewTest.Views.Home.IndexModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
List
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<table width="500" border="0" cellpadding="0" cellspacing="0">
<%Html.RenderPartial("IndexUserRow",new PartialViewTest.Views.Home.IndexUserRowModel(true,null,0));%>
<%((List<PartialViewTest.Models.UserInfo>)Model.UserInfo).ForEach((u) => Html.RenderPartial("IndexUserRow", new PartialViewTest.Views.Home.IndexUserRowModel(false, u, Model.PageNumber))); %>
</table>
<p>
<%Html.RenderPartial("Pager", new PartialViewTest.Views.Shared.PagerModel(Model.PageSize, Model.PageNumber, Model.TotalCount));%>
</p>
</asp:Content>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PartialViewTest.Views.Shared.PagerModel>" %>
<%= PartialViewTest.Views.Heplers.If(
Model.HasPreviousPage,
() => Html.ActionLink("上一页", "List", new { page = Model.PageNumber - 1 }),
() => "上一页"
)%>
|
<% for (int i = 1; i <= (Model.TotalCount - 1) / Model.PageSize + 1; i++)
{%>
<% if (i > 1) { %> | <%} %>
<% if (i == Model.PageNumber)
{ %>
<strong><%=i%></strong>
<%}
else
{ %>
<%=Html.ActionLink(i.ToString(), "List", new { page=i})%>
<%} %>
<%} %>
|
<%= PartialViewTest.Views.Heplers.If(
Model.HasNextPage,
() => Html.ActionLink("下一页", "List", new { page = Model.PageNumber + 1 }),
() => "下一页"
)%>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PartialViewTest.Models;
using System.Collections.Generic;
namespace PartialViewTest.Views.Shared
{
public class PagerModel
{
public PagerModel(int pageSize, int pageNumber, int totalCount)
{
this.HasPreviousPage = pageNumber > 1 ? true : false;
this.HasNextPage = pageNumber * pageSize < totalCount ? true : false;
this.MaxPageNumber = (totalCount - 1) / pageSize + 1;
this.PageSize = pageSize;
this.PageNumber = pageNumber;
this.TotalCount = totalCount;
}
public bool HasPreviousPage
{
get;
private set;
}
public bool HasNextPage
{
get;
private set;
}
public int MaxPageNumber
{
get;
private set;
}
public int PageSize
{
get;
private set;
}
public int PageNumber
{
get;
private set;
}
public int TotalCount
{
get;
private set;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace PartialViewTest.Views
{
public static class Heplers
{
public static string If(bool condition,Func<string> value,Func<string> elseValue )
{
return condition ? value() : elseValue();
}
public static void ForEach(int includeMin, int excludeMax, Action<int> action)
{
for (int i = includeMin; i < excludeMax; i++) action(i);
}
public static void If(bool condition, Action action, Action elseAction)
{
if (condition)
action();
else if (elseAction != null)
elseAction();
}
public static void ForEach<T>(this IEnumerable<T> items,Action<T> action)
{
foreach (var item in items) action(item);
}
public static void ForEach<T>(this IEnumerable<T> items, Action<T,int> action)
{
int index = 0;
foreach (var item in items) action(item,index++);
}
}
}
在Site.css最后加了
.even
{
color:#000;
}
.odd
{
color:#f00;
}
好了,以上是我全部代码,不正确的地方,请指正.