zoukankan      html  css  js  c++  java
  • 【MVC框架】——View和Controller之间的传值

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





  • 相关阅读:
    .NET开发不可不知、不可不用的辅助类(一)
    .NET开发不可不知、不可不用的辅助类(三)(报表导出终结版)
    如何获取类或属性的自定义特性(Attribute)
    如何使用C#进行Visio二次开发
    列表查询组件代码, 简化拼接条件SQL语句的麻烦
    强大的模板引擎开源软件NVelocity
    自己编写的操作实体类的分页控件, 实现页码层与数据库的具体的信息隔离
    代码生成工具随笔(1) 关于代码生成器
    老歌新唱使用VB6开发的ActiveX实现.NET程序的混淆加密
    VB6中如何使用C#开发的WebService进行开发
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6893401.html
Copyright © 2011-2022 走看看