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

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>

  

posted on 2017-06-21 19:00  lkjhgfdsa123  阅读(137)  评论(0编辑  收藏  举报