Original link: http://blog.csdn.net/fanbin168/article/details/44803437
ViewBag
The use and difference of ViewBag, ViewData and TempData in MVC3
At the beginning of the MVC3, the view data can be accessed through the ViewBag property, and ViewData is used in the MVC2. The use of ViewData is retained in the MVC3. ViewBag is dynamic, and ViewData is a typical character (Dictionary).
The difference between ViewBag and ViewData:
ViewBag is no longer a key-value-pair structure of a dictionary, but dynamically dynamic type, which is dynamically parsed when the program is running. So no type conversion is required to get its data in the view
| ViewData |
ViewBag |
| It's a collection of Key/value dictionaries. |
It is dynamic type pair like |
| It's from ASP.net MVC 1. |
asp.net MVC3 only |
| Based on ASP.net 3.5 framework |
Based on ASP.net 4.0 and the. NET Framework |
| ViewData faster than ViewBag. |
ViewBag is slower than ViewData. |
| You need to convert the appropriate type when querying data in ViewPage |
Type conversion is not required when querying data in ViewPage |
| There are some type conversion codes |
Better readability |
Contorller [CSharp] View plain copy <pre class= "CSharp" name= "code" >using NewOjbect.Models; using system; using system.collections.generic; using System.Linq; using system.web; using system.web.mvc; namespace NewOjbect.Controllers { public class homecontroller : controller { public actionresult index () { ViewBag.UserName = "Salt-free Sea"; ViewBag.Age = "; " viewbag.gender = 1; string[] Itmes = new string[] { "China", "America", "Germany" }; viewbag.itemsa = itmes;// viewbag is a new dynamic type keyword wrapper //viewdata["Items"] = items; return view (); } } }
View
[HTML] view plain copy <pre class= "html" name= "code" >