在asp.net2.0的网页开发模式下,我们一般通过直将访问页面控件, 将值写入到页面, 但在Asp.net MVC模式下,已不能在Controller中再去访问页面控件了,要如何才能给View页面传值呢?在Controller中有两个字典(ViewData和TempData)来实现View之间的值传递,Controller通过ViewData,TempData传通到ViewResult中, 然后再由ViewResult传递到ViewPage中来实现值传递的。
1.TempData和ViewData的应用
ViewData只对当前Action有效,而TempData有点类似于Session, 可在所有View访问, 一般用于记录错误信息.
Action代码:
[csharp] view plain copy print ?
- public ActionResult Index()
- {
- ViewData["Message"] = "Welcome to ASP.NET MVC!";
- return View();
- }
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
页面代码:
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
TempData使用方式与View使用方式一致.
2. ViewData与TextBox实现自动绑定
利用HtmlHelper创建TextBox时,使用名称与ViewData中的Key一致, 就会自动实现值绑定,如:
Name:<%= Html.TextBox("name") %>
名称不相同的情况下,也可以利用TextBox的重载传值:
Name:<%= Html.TextBox("name", ViewData["Nm"]) %>
3.View向Controller传值
1). 利用Action参数
[csharp] view plain copy print ?
- <form name="form1" action="/Home/Index" method="post">
- Name:<input type="text" name="name" /><br />
- Sex: <input type="text" name="sex" />
- <input type="submit" value="submit" />
- </form>
- <%
- if (ViewData["name"] != null)
- {
- Response.Write("your name is:" + ViewData["name"] + ", your sex is:" + ViewData["sex"]);
- }
- %>
- :
- public ActionResult Index(string name, string sex)
- {
- ViewData["Message"] = "Welcome to ASP.NET MVC!";
- ViewData["name"] = name;
- ViewData["sex"] = sex;
- return View();
- }
<form name="form1" action="/Home/Index" method="post">
Name:<input type="text" name="name" /><br />
Sex: <input type="text" name="sex" />
<input type="submit" value="submit" />
</form>
<%
if (ViewData["name"] != null)
{
Response.Write("your name is:" + ViewData["name"] + ", your sex is:" + ViewData["sex"]);
}
%>
:
public ActionResult Index(string name, string sex)
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
ViewData["name"] = name;
ViewData["sex"] = sex;
return View();
}
[html] view plain copy print ?
- 2).利用Request.From或Request.QueryString
2).利用Request.From或Request.QueryString
[csharp] view plain copy print ?
- public ActionResult Index()
- {
- ViewData["Message"] = "Welcome to ASP.NET MVC!";
- ViewData["name"] = Request.Form["name"];
- ViewData["sex"] = Request.Form["sex"];
- return View();
- }
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
ViewData["name"] = Request.Form["name"];
ViewData["sex"] = Request.Form["sex"];
return View();
}
3). 利用FormCollection获取页面值
[csharp] view plain copy print ?
- public ActionResult Index(FormCollection form)
- {
- ViewData["Message"] = "Welcome to ASP.NET MVC!";
- User u=new User();
- u.Name = form["Name"];
- u.Password = form["Password"];
- return View(u);
- }
public ActionResult Index(FormCollection form)
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
User u=new User();
u.Name = form["Name"];
u.Password = form["Password"];
return View(u);
}
4.传递强类型
1).添加一个传递强类型Model的Action
[csharp] view plain copy print ?
- public ActionResult ModelDemo()
- {
- User u= new User() { UserName="li", Password="abcde" };
- return View(u);
- }
public ActionResult ModelDemo()
{
User u= new User() { UserName="li", Password="abcde" };
return View(u);
}
对应的View也需要继随于ViewPage<User>, 对应代码如下:
[html] view plain copy print ?
- <p>
- <%User u = (User)ViewData.Model;%>
- UserName:
- <%= Html.Encode(u.UserName) %>
- </p>
- <p>
- Password:
- <%= Html.Encode(u.Password) %>
- </p>
<p>
<%User u = (User)ViewData.Model;%>
UserName:
<%= Html.Encode(u.UserName) %>
</p>
<p>
Password:
<%= Html.Encode(u.Password) %>
</p>