在MVC中,Controller运行一个能够说是路由功能。它通过View传过来的数据,来决定应该调用哪一个Model,相同会把Model处理完的数据传给View,所以就总是涉及到Controller和View的传值,那么它们之间是怎么传值的呢?
Controller向View传值
1、使用ViewBag
Controller
<span style="font-family:KaiTi_GB2312;font-size:18px;">public ActionResult Index()
{
ViewBag.Message = "欢迎使用 ASP.NET MVC!";
return View();
}</span>
View
<span style="font-family:KaiTi_GB2312;font-size:18px;">@{
ViewBag.Title = "主页";
}
<h2>@ViewBag.Message</h2>
<p>
若要了解有关 ASP.NET MVC 的很多其它信息,请訪问 <a href="http://asp.net/mvc" title="ASP.NET MVC 站点">http://asp.net/mvc</a>。
</p></span>
ViewBag传过来的Message信息将会传递给<h2>@ViewBag.Message</h2>。所实现的效果就是
2、使用ViewData
Controller
<span style="font-family:KaiTi_GB2312;font-size:18px;">public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
} </span>
View
<span style="font-family:KaiTi_GB2312;font-size:18px;">
<h2><%=Html.Encode(ViewData["Message"])%></h2>
<p>
若要了解有关 ASP.NET MVC 的很多其它信息,请訪问 <a href="http://asp.net/mvc" title="ASP.NET MVC 站点">http://asp.net/mvc</a>。
</p></span>
所实现的效果是同样的。
3、使用TempData
Controller
<span style="font-family:KaiTi_GB2312;font-size:18px;">public ActionResult Index()
{
TempData["Message"] = "Welcome to ASP.NET MVC!";
return View();
} </span>
View
<span style="font-family:KaiTi_GB2312;font-size:18px;">
<h2><%=Html.Encode(TempData["Message"])%></h2>
<p>
若要了解有关 ASP.NET MVC 的很多其它信息。请訪问 <a href="http://asp.net/mvc" title="ASP.NET MVC 站点">http://asp.net/mvc</a>。
</p></span>
4、使用Model
Controller
<span style="font-family:KaiTi_GB2312;font-size:18px;">public ActionResult ModelDemo()
{
User u= new User() { UserName="li", Password="abcde" };
return View(u);
} </span>
<span style="font-family:KaiTi_GB2312;font-size:18px;"><p>
<%User u = (User)ViewData.Model;%>
UserName:
<%= Html.Encode(u.UserName) %>
</p>
<p>
Password:
<%= Html.Encode(u.Password) %>
</p> </span>
ViewData是Key/Value字典集合。在MVC1中就有了,ViewData传值比ViewBag要快;ViewBag是dynamic类型对象。从MVC3才開始出现。比ViewData传值慢。可是可读性好。
ViewData仅仅能用于当前Action中,而TempData类似于Session,能够跨Action进行訪问,一般用于存储错误信息。
Model传递强类型,所以在创建视图时,须要创建强视图。
View向Controller传值
1、通过Request.Form读取表单数据
View
<span style="font-family:KaiTi_GB2312;font-size:18px;"><% using (Html.BeginForm("ActionName", "ControllerName"))
{ %>
UserName:<% Html.TextBox("UserName"); %>
Password:<% Html.TextBox("Password"); %>
<%} %>
</span>
Controller
<span style="font-family:KaiTi_GB2312;font-size:18px;">[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ActionName()
{
string username = Request.Form["UserName"];
string password = Request.Form["Password"];
return View();
}
</span>
2、通过FormCollection读取表单数据
View
<span style="font-family:KaiTi_GB2312;font-size:18px;"><% using (Html.BeginForm("ActionName", "ControllerName"))
{ %>
UserName:<% Html.TextBox("UserName"); %>
Password:<% Html.TextBox("Password"); %>
<%} %></span>
Controller
<span style="font-family:KaiTi_GB2312;font-size:18px;">[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ActionName(FormCollection formCollection)
{
string username = formCollection["UserName"];
string password = formCollection["Password"];
return View();
}
</span>
总结
页面传值会用到各种方法,那么页面和控制器间的传值相同会有非常多方法。View和Controller传值是不可避免的。熟练掌握它们之间的传值方法,有利于更流利的开发。