asp.net mvc中Controller向View传值方式总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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传值方式总结的主要内容,如果未能解决你的问题,请参考以下文章
Asp.net MVC中 Controller 与 View之间的数据传递