视图数据可以通过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>
<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 中以备循环使用。
 
 
文章出处:http://www.cnblogs.com/zhangqs008/archive/2012/08/11/2802207.html
 
 

MVC ViewData和ViewBag的更多相关文章

  1. asp.net mvc ViewData 和 ViewBag区别,TempData

    ViewData 和 ViewBag都是页面级别的生命周期,TempData--Passing data between the current and next HTTP requests Temp ...

  2. ASP.net MVC+ViewData VS ViewBag

         在使用MVC框架的过程中,往界面传值,我们使用的ViewData.如ITOO部分代码图解:      当然除了ViewData,我们还能够使用同卵兄弟(ViewBag)来完毕相同的功能,详情 ...

  3. MVC ViewData和ViewBag[转]

    转自:http://blog.csdn.net/a497785609/article/details/7854402#t0       视图数据可以通过ViewBag属性访问,它主要是为了从Contr ...

  4. MVC ViewData与ViewBag的区别

    一.ViewData 1.ViewData派生自ViewDataDictionary,所以它具有字典的属性,例如:ContainsKey .Add .Remove 和 Clear : 2.字典键值是字 ...

  5. MVC ViewData和ViewBag 参数名一样表示同一个值

    @ViewData["MenuName"] 和 @ViewBag.MenuName 表示同一个值

  6. 在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下:

    Asp.net MVC中的ViewData与ViewBag ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从Asp.net MVC 1 就有了 ASP. ...

  7. Asp.net MVC的ViewData与ViewBag以及TemplateData的使用与区别

    ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从Asp.net MVC 1 就有了 ASP.NET MVC3 才有 基于Asp.net 3.5 fram ...

  8. asp.net mvc中ViewData、ViewBag和TempData的详解

    一.ViewData和ViewBag 1.ViewData和ViewBag都是从Action向View传递数据的方式,当然还有其他方式如向View传递Model. 2.ViewData页面查询数据时需 ...

  9. .NET MVC TempData、ViewData、ViewBag

    说明: 原文作者贤新 原文地址:http://www.cnblogs.com/chenxinblogs/p/4852813.html ViewData和ViewBag主要用于将数据从控制器中传递到视图 ...

随机推荐

  1. RH的NFS配置--简单OK

    参照文档: http://wenku.baidu.com/link?url=SAcDvj8WtBd8dunC7P6FTFADYYVzzxhOiNJqbgr-aGTZovM0lHg-wbYgv9I3Lu ...

  2. 要将PYTHON应用于工作啦

    分析同事在线答疑的数据,考评模型还未最终给出: import time import sys import optparse #操作代码和同事名对应的文件 opfile = 'op_name.txt' ...

  3. 纯CSS打造可折叠树状菜单

    1:Html代码 <li> <label for="subsubfolder1">下级</label> <input id="s ...

  4. AlarmManager.setRepeating将不再准确

    背景: 当我们想让Android应用程序定时为做一件工作时,我们往往会在一个BroadcastReceiver中使用AlarmManager.setRepeating()方法来实现.在API 19(即 ...

  5. mysql表分区、查看分区

    原文地址:http://blog.csdn.net/feihong247/article/details/7885199 一.       mysql分区简介 数据库分区 数据库分区是一种物理数据库设 ...

  6. Hadoop 新 MapReduce 框架 Yarn 详解

    Hadoop 新 MapReduce 框架 Yarn 详解: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ Ap ...

  7. lambda -- Filter Java Stream to 1 and only 1 element

    up vote10down votefavorite I am trying to use Java 8 Streams to find elements in a LinkedList. I wan ...

  8. HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  9. MySQL5.5 所支持的存储引擎

    本博文的主要内容有 .存储引擎的概念 .MySQL5.5 所支持的存储引擎 .操作默认存储引擎 .选择存储引擎 与其他的数据库软件不同,MySQL数据库软件提供了一个名为存储引擎的概念,由于存储引擎是 ...

  10. 【转】Gabor 入门

    Computer Vision Tutorials Search Primary Menu Skip to content Tutorials Search for:   Gabor Filters ...