视图数据可以通过ViewBag属性访问,它主要是为了从Controller到view进行传值用的,类似有所使用的ViewData[] 字典类。对于ViewBag是如此的强大,意味着你能动态的set/get 值,增加任何数量的的额外字段而不需要强类型的检测。如:
Controller
public
ActionResult Index()
{
List< string >
colors = new
List< string >();
colors.Add( "red" );
colors.Add( "green" );
colors.Add( "blue" );
ViewData[ "listColors" ]
= colors;
ViewData[ "dateNow" ]
= DateTime.Now;
ViewData[ "name" ]
= "Hajan" ;
ViewData[ "age" ]
= 25;
return
View();
}
|
Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public
ActionResult Index()
{
List< string >
colors = new
List< string >();
colors.Add( "red" );
colors.Add( "green" );
colors.Add( "blue" );
ViewBag.ListColors = colors;
ViewBag.DateNow
= DateTime.Now;
ViewBag.Name
= "Hajan" ;
ViewBag.Age
= 25;
return
View();
}
|
你和上面的对比 你看见了不同吗?
View
对应前台视图:
1.使用ViewData
<p>
My name is
<b>@ViewData["name"]
</b>,
<b>@ViewData["age"]
</b>years old.
<br
/>
I like the following colors:
</p>
<ul
id="colors">
@foreach (var
color in ViewData["listColors"]
as List<string>)
{
<li><font
color="@color">@color
</font></li>
}
</ul>
<p>
@ViewData["dateNow"]
</p>
2.使用ViewBag
<p>
My name is
<b>@ViewBag.Name</b>,
<b>@ViewBag.Age</b> years old.
<br
/>
I like the following colors:
</p>
<ul
id="colors">
@foreach (var color
in ViewBag.ListColors)
{
<li><font
color="@color">@color</font>
</li>
}
</ul>
<p>
@ViewBag.DateNow
</p>
效果图:
ViewBag、ViewData 和 TempData 的区别:
ViewData 和 TempData 都可以传递弱类型数据,区别如下:
ViewData 只在当前 Action 中有效,生命周期和 View 相同;
TempData 的数据至多只能经过一次Controller传递,并且每个元素至多只能被访问一次,访问以后,自动被删除。
TempData 一般用于临时的缓存内容或抛出错误页面时传递错误信息,可以将TempData 在使用之前存储到相应的 ViewData 中以备循环使用。
分享到:
相关推荐
本文分析了.NET MVC中ViewData,ViewBag和TempData的区别。分享给大家供大家参考。具体分析如下: ViewData和TempData是字典类型,赋值方式用字典方式,如: 代码如下:ViewData[“myName”] ViewBag是动态类型,使用...
本系列文章共计20篇,主要包括ASP.NET Core MVC项目创建,启动运行,以及命名约定,创建控制器,视图,模型,接收参数,传递数据ViewData,ViewBag,路由,页面布局,wwwroot和客户端库,Razor语法,...
4.ViewBag is not onle available but it is dynamic.Like ViewBag = ViewBag + dynamic feature around the ViewData dictionary. 5.Jqeury : it provides better support for jquery like Jquery Mobile. 6.MVC3 ...
当我还是一个初学者的ASP.NET MVC中,我遇到了一个问题:有多少种方法你知道通过多个模型视图中的?在那个时候,我就知道只有两种方式,然后我学到了一些其他的方法来达到同样的。在这篇文章中,我将分享我的学习...
分页、数据传输方式(ViewBag、ViewData、TempData、Model)、各种ActionResult、四种Filter(IAuthorizationFilter、IActionFilter、IResultFilter、IExceptionFilter)、HtmlHelper、路由机制、验证、layout;...
this is index ViewBag.User1- ViewData.User2- TempData.User3- ViewBag.User1- ViewBag.User1-
往前台视图传的类型是List,把SelectListItem选中项的Selected属性设置为true,再把该类型对象实例放到ViewBag,ViewData或Model中传递给前台视图。 通过遍历List类型对象实例 □ 控制器 public ActionResult ...
新的ViewBag属性 新的ActionResult类型 Model Validation JSON绑定支持 Dependncy Injection html5 Css3 部分页的输出缓存 htmlHelper的增强 NuGet fires.Add(new HandErrorAttribute()); ...
ViewData TemData 会议03:创建模型 建立模型类 创建数据库。 将数据库SqlServer与ADO.Net连接。 第四节:考试 第五节:练习分区并为Areas Admin集成模板Sb Admin 2 在网站中分割管理员分区 集成的Sb Admin 2...
用于查看传递数据的控制器:* ViewBag,ViewData,TempData *在ActionResult中创建的模型实例,作为View()参数传递,在指定的@model app.Models.Person视图中,在其余页面中,我们使用Model.propertiest / ...
您将学到的知识了解ASP NET Core MVC的基础知识5了解ASP NET的工作原理学习ASP NET CORE MVC中的表单和验证学习ViewModels,ViewBag,ViewData等。了解如何通过ASP NET Core应用程序发送电子邮件最佳实践通过作业...