`
380071587
  • 浏览: 446110 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

MVC ViewData和ViewBag

 
阅读更多
视图数据可以通过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; //colors is List
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的区别浅析

    本文分析了.NET MVC中ViewData,ViewBag和TempData的区别。分享给大家供大家参考。具体分析如下: ViewData和TempData是字典类型,赋值方式用字典方式,如: 代码如下:ViewData[“myName”] ViewBag是动态类型,使用...

    ASP.NET Core MVC从入门到精通系列文章PDF版

    本系列文章共计20篇,主要包括ASP.NET Core MVC项目创建,启动运行,以及命名约定,创建控制器,视图,模型,接收参数,传递数据ViewData,ViewBag,路由,页面布局,wwwroot和客户端库,Razor语法,...

    mvc3,4,5区别于联系

    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 MVC4/5的MVC视图多模型

    当我还是一个初学者的ASP.NET MVC中,我遇到了一个问题:有多少种方法你知道通过多个模型视图中的?在那个时候,我就知道只有两种方式,然后我学到了一些其他的方法来达到同样的。在这篇文章中,我将分享我的学习...

    .net MVC、.Net core+Linux视频教程

    分页、数据传输方式(ViewBag、ViewData、TempData、Model)、各种ActionResult、四种Filter(IAuthorizationFilter、IActionFilter、IResultFilter、IExceptionFilter)、HtmlHelper、路由机制、验证、layout;...

    Web MVC实操案例; LH时期,测试抽象项目集合 1

    this is index ViewBag.User1- ViewData.User2- TempData.User3- ViewBag.User1- ViewBag.User1-

    ASP.NET MVC中为DropDownListFor设置选中项的方法

    往前台视图传的类型是List,把SelectListItem选中项的Selected属性设置为true,再把该类型对象实例放到ViewBag,ViewData或Model中传递给前台视图。  通过遍历List类型对象实例 □ 控制器 public ActionResult ...

    CodematicDemoMVC

    新的ViewBag属性 新的ActionResult类型 Model Validation JSON绑定支持 Dependncy Injection html5 Css3 部分页的输出缓存 htmlHelper的增强 NuGet fires.Add(new HandErrorAttribute()); ...

    WebProgramming_2020-2021:Web编程课程18CT111中的存档练习

    ViewData TemData 会议03:创建模型 建立模型类 创建数据库。 将数据库SqlServer与ADO.Net连接。 第四节:考试 第五节:练习分区并为Areas Admin集成模板Sb Admin 2 在网站中分割管理员分区 集成的Sb Admin 2...

    ASPNET身份管理

    用于查看传递数据的控制器:* ViewBag,ViewData,TempData *在ActionResult中创建的模型实例,作为View()参数传递,在指定的@model app.Models.Person视图中,在其余页面中,我们使用Model.propertiest / ...

    AppointmentScheduling

    您将学到的知识了解ASP NET Core MVC的基础知识5了解ASP NET的工作原理学习ASP NET CORE MVC中的表单和验证学习ViewModels,ViewBag,ViewData等。了解如何通过ASP NET Core应用程序发送电子邮件最佳实践通过作业...

Global site tag (gtag.js) - Google Analytics