码迷,mamicode.com
首页 > Web开发 > 详细

asp.net mvc中Controller向View传值方式总结

时间:2017-06-21 19:57:15      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:html   mod   arch   对象   get   UI   tostring   var   类型   

http://www.cnblogs.com/gaopin/archive/2012/11/13/2767515.html

http://www.cnblogs.com/a164266729/p/4917202.html

 

 

  //1.ViewData传递  ViewData是Key/Value对
            ViewData["str1"] = "hello 1";
            //ViewData.str2 = "world 2";
            //2.ViewBag传递  动态 且是强类型的 可以传对象
            ViewBag.str2 = "world2";

         //3.ViewBag传对象
            ViewBag.Date = DateTime.Now;
            ViewBag.stu = new Student() { name="123",id="444"};
            //可以传匿名对象但是是无法 点出来的
            ViewBag.user = new { age = 12, uName = "zhangsan" }  
;
          //ViewData.Model传递对象 或者集合 使用ViewData.Model传值需添加强类型(Model)

            //使用Model(等价于ViewData.Model)最好是用强类型视图
            //否则还不如用ViewBag来传 还不用强转
            ViewData.Model = new Student() { name="1qqqqqqqqqq",id="223"};
            //ViewData.Model = new Student() { name="jjjjjjjj",id="2014294"};

  

<div>
        <h1>@ViewData["str1"]</h1>
        <h2>@ViewBag.str2</h2>
        @*<h3>@ViewBag.stu.name</h3>*@
        <h2>@ViewBag.Date.ToString()</h2>
        H2ssssss
        <h2>@ViewBag.stu.name</h2>
        <h2>@ViewBag.user</h2>


        @{var stu = (LTeasyOA.UI.Portal2.Controllers.HomeController.Student)Model;}
        <h2>@stu.id</h2>
        <h2>@((LTeasyOA.UI.Portal2.Controllers.HomeController.Student)Model).name</h2>/////错误
    </div>

  

asp.net mvc中Controller向View传值方式总结

标签:html   mod   arch   对象   get   UI   tostring   var   类型   

原文地址:http://www.cnblogs.com/lt123/p/7061284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!