首页 > 代码库 > asp.net mvc中Controller向View传值方式总结
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>
asp.net mvc中Controller向View传值方式总结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。