• ASP.NET MVC视图和控制器之间的传值总结(一)


    一、Control往View传递值

         1.1 ViewBag

              ViewBag的后面接的是动态对象,它会在程序运行的时候动态解析。具体用法为ViewBag.Key,Key为Data在View中的唯一识别。

              ViewBag其实本质就是ViewData,只是多了层Dynamic控制。他们的Key是共用的,如ViewBag.name其实和ViewData["name"]源代码是一样的,

              修改了其中任何一个,两个都会被修改。ViewData和ViewBag中的值可以互相访问。

              代码:

             Control端:

          

    public ActionResult Index2()
            {
                ViewBag.name = "张三";
                ViewData["name"] = "李四";
                return View();
            }
    

      

             View端:

    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index2</title>
    </head>
    <body>
        <div>
            <span>ViewBag:   姓名:@ViewBag.name</span>
            <span>ViewData: 姓名:@ViewData["name"]</span>
        </div>
    </body>
    </html>
    

      结果:

         ViewBag: 姓名:李四 ViewData: 姓名:李四

      1.2 ViewData

             ViewData它key/Value的键值对。

          Control端:

    ViewData["name"] = "张三";

          View端:

    <span>年龄:@TempData["age"]</span>

          ViewData和ViewBag的主要区别:ViewPage查询数据的时候,ViewData需要做数据类型的转换(必须显式转换),而ViewBag不需要做一些数据类型的转换(隐式转换)。

         Control端:

       

            public ActionResult Index()
            {
                Student stu = new Student();
                stu.name = "张三";
                stu.age = 20;
                ViewBag.student = stu;
                ViewData["student"] = stu;
                return View();
            }
    

      

         View端:

    @{
        ViewBag.Title = "Index";
    }
    
    <span>姓名:@ViewBag.student.name</span> <span>年龄:@ViewBag.student.age</span>
    @{
        var stu = @ViewData["student"] as @MVC传值.Models.Student;
    }
    <span>姓名:@stu.name</span> <span>年龄:@stu.age</span>
    <a href="JumpMethod" >跳转到Test1View</a>
    

       1.3 TempData 

    TempData只保存到下一个请求中,下一个请求完了之后,TempData就会被删除了。

    TempData每一项即每一个Key对应的项次只能被(View或者Controller)使用一次,就会被销毁。

    Request的定义从宏观上看就是页面刷新了一次

    至于TempData的生存周期请参考:http://www.cnblogs.com/jhxk/articles/4773527.html

    Control端:

           public ActionResult Index()
            {
                TempData["age"] = 20;
                return View();
            }
            public ActionResult JumpMethod()
            {
                //TempData["age"] = 21;
                return RedirectToAction("Test1");
            }
            public ActionResult Test1()
            {
                return View();
            }
    

      

    View端:

    1 <span>姓名: @ViewBag.name</span>&nbsp;<span>年龄:@TempData["age"]</span>
  • 相关阅读:
    微信小程序的scheme码
    微信小程序的简单总结(uni-app)
    ES7-ES11新特性
    Promise 总结
    uni-app创建项目及使用 vant-weapp
    vscode 插件整理
    el-upload 组件总结
    从输入URL到页面显示过程中发生了什么
    实验 1:Mininet 源码安装和可视化拓扑工具
    2020软件工程第一次作业
  • 原文地址:https://www.cnblogs.com/cherish836138981/p/6586597.html
Copyright © 2020-2023  润新知