【MVC框架】——View和Controller之间的传值

6 篇文章 3 订阅

    在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>

 

 

View

 

<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传值是不可避免的,熟练掌握它们之间的传值方法,有利于更流利的开发。

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 32
    评论
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值