我们前面都使用RenderView("Index");这种方式来显示
RenderView的重载:
RenderView(
string
viewName);
RenderView(
string
viewName,
object
viewData);
RenderView(
string
viewName,
string
masterName);
RenderView(
string
viewName,
string
masterName,
object
viewData);
我们常用的当然就是第一种
第二种 RenderView( string viewName, object viewData);是在显示view时附加一个ViewData
如:
RenderView("Index", new

{
name = "重典",
sex = true
});
我们就可以在相应的View(即Index.aspx)中调用<%=ViewData["name"]%>来得到它的值
RenderView( string viewName, string masterName);
则是除了Viewname之外还指定了母板页
如果程序写为
RenderView(
"
index
"
,
"
layoutpage
"
);
则是显示index这个View , 但是母板页使用/Views/Shared/layoutpage.master
最后一个重载是前两者的结合,这里不多说了
更多的View传值方式
前面说了向VIew传值可以用ViewData或者是TempData
这里我们介绍ViewData传值的另一种的方式
与Models绑定
这是我们第一次介绍Models,其实Models就是一个数据模型,比如,用户,人,访问记录等
我们建立一个Person类
namespace
MvcApplication2.Models

{
public class Person

{
string _name;


public string Name {

get { return _name; }

set { _name = value; }
}
bool _sex;


public bool Sex {

get { return _sex; }

set { _sex = value; }
}
}
}
类中有Name与Sex两个属性
我们仍然用Eice的Index这个Action来书写示例
首先我们实例化一个Person并将之传给View
Person p
=
new
Person()

{
Name = "邹健",
Sex = true
}
;
RenderView(
"
Index
"
, p);
接下来我们更改View中Eive/Index.aspx.cs
将它的更改如下
public
partial
class
Index : ViewPage
<
Person
>

{
}
也就是原来Index是继承ViewPage的而现在继承了ViewPage<Person>这个泛型
接下来我们可以在Index.aspx中写类似以下语句了
<%=
ViewData.Name
%>
<%=
ViewData.Sex
%>
当然您也可以不写ViewPage<Person>而还像从前一样继承于ViewPage
那么访问方法就是过去的
<%=
ViewData[
"
Name
"
]
%>
<%=
ViewData[
"
Sex
"
]
%>
回首望去MS讲的内容与标题反了...Eat去了...每天都为吃发愁...
Asp.net Mvc Framework 系列
RenderView的重载:




第二种 RenderView( string viewName, object viewData);是在显示view时附加一个ViewData
如:






RenderView( string viewName, string masterName);
则是除了Viewname之外还指定了母板页
如果程序写为

最后一个重载是前两者的结合,这里不多说了
更多的View传值方式
前面说了向VIew传值可以用ViewData或者是TempData
这里我们介绍ViewData传值的另一种的方式
与Models绑定
这是我们第一次介绍Models,其实Models就是一个数据模型,比如,用户,人,访问记录等
我们建立一个Person类


























我们仍然用Eice的Index这个Action来书写示例
首先我们实例化一个Person并将之传给View







将它的更改如下




接下来我们可以在Index.aspx中写类似以下语句了


那么访问方法就是过去的


回首望去MS讲的内容与标题反了...Eat去了...每天都为吃发愁...
Asp.net Mvc Framework 系列