我在MVC 3中看到了ViewBag。这和MVC 2中的ViewData有什么不同?
当前回答
这里ViewData和ViewBag都用于将数据从控制器传递到视图。
1. 显示数据
——ViewData是字典对象,从ViewDataDictonary类派生。
数据只允许一个请求,当页面重定向发生时ViewData值被清除。
——ViewData值必须在使用前输入。
示例:在控制器中
public ActionResult PassingDatatoViewWithViewData()
{
ViewData["Message"] = "This message shown in view with the ViewData";
return View();
}
在视图
@ViewData["Message"];
——ViewData是Key和Value这样的一对,Message是Key,在倒逗号中Value是Value。
数据是简单的,所以我们不能在这里使用类型转换,如果数据是复杂的,那么使用类型转换。
public ActionResult PassingDatatoViewWithViewData()
{
var type= new List<string>
{
"MVC",
"MVP",
"MVVC"
};
ViewData["types"] = type;
return View();
}
在视图中数据可以提取为
<ul>
@foreach (var items in (List<string>)ViewData["types"])
{
<li>@items</li>
}
</ul>
2. ViewBag
——ViewBag使用动态特性。ViewBag包装了ViewData。
—在ViewBag类型铸造是必需的。
——与ViewData相同,如果重定向发生,值将变为null。
例子:
public ActionResult PassingDatatoViewWithViewBag()
{
ViewData.Message = "This message shown in view with the ViewBag";
return View();
}
在视图
@ViewBag.vbMessage
—对于复杂类型使用ViewBag
public ActionResult PassingDatatoViewWithViewBag()
{
var type= new List<string>
{
"MVC",
"MVP",
"MVVC"
};
ViewBag.types = type;
return View();
}
在视图中数据可以提取为
<ul>
@foreach (var items in ViewBag.types)
{
<li>@items</li>
}
</ul>
主要的区别是ViewBag不需要类型转换,而ViewData需要类型转换。
其他回答
ViewData用于将数据从控制器传递给ViewData,它是从viewdatdictionary类派生出来的。仅对当前请求可用。仅要求复杂数据类型的类型转换,并检查null值以避免错误。如果发生重定向,则其值为null
ViewBagViewBag是一个动态属性,它利用了c# 4.0中的新动态特性。它也仅对当前请求可用。如果发生重定向,则其值为null
所有的答案都表明ViewBag和/或ViewData是将数据从控制器传递到视图,这是错误的信息。两者都非常有用,可以将数据从视图传递到布局或局部视图(或ViewComponents等)。它不是控制器专属的。
默认的asp.net示例在布局页面中有:
<title>@ViewData["Title"] - MyApp</title>
在任何情况下
ViewData["Title"] = "Details";
然后,问这个问题:“ViewBag和ViewData之间有什么区别?”
最显著的区别是ViewData是一个强类型字典 ViewBag是一个动态类型。
注意里面的数据IS the SAME
ViewData["Title"] = "MyTitle";
ViewBag.Title; // returns "MyTitle";
什么时候用这个或那个?
ViewBag doesn't support not valid C# names. you can't access ViewData["Key With Space"] with ViewBag ViewBag.Something is dynamic and you may have problems when calling methods (like extension methods) that needs to know the exact parameter at compile time. ViewBag can check for nulls syntactical cleaner: ViewBag.Person?.Name ViewData have all the properties of a Dictionary like ContainsKey, Add, etc. so you can use ViewData.Add("somekey", "somevalue") keep in mind it might throw exceptions. Using ViewData on views needs TypeCasting while ViewBag don't.
知道了细微的差别,使用其中一种更像是一种口味偏好。
通常你可以想到ViewBag。ViewData别名的任意键["AnyKey"]
下面是关于ViewData, ViewBag, TempData和Session的点对点差异。 信贷/ askforprogram复制。在这里,遵循我没有提到的代码示例的链接。
ViewData in MVC ViewData is property of ControllerBase class. ViewData is a type of dictionary object. ViewData is key-value dictionary collection. ViewData was introduced in MVC 1.0 version. ViewData works with .Net framework 3.5 and above. Need to do type conversion of code while enumerating. ViewData object keeps data only for current request. ViewBag in MVC ViewBag is property of ControllerBase class. ViewBag is a type of dynamic object. ViewBag is a type of object. ViewBag was introduced in MVC 3.0 version. ViewBag works with .Net framework 4.0 and above. ViewBag uses property and handles it, so no need to do type conversion while enumerating. ViewBag object keeps data only for current request. TempData in MVC TempData is property of ControllerBase class. TempData is a type of dictionary object. TempData is key-value dictionary collection. TempData was introduced in MVC 1.0 version. TempData works with .Net framework 3.5 and above. Need to do type conversion of code while enumerating. TempData object is used to data between current request and subsequent request. Session in MVC Session is property of Controller(Abstract Class). Session is a type of HttpSessionStateBase. Session is key-value dictionary collection. Session was introduced in MVC 1.0 version. TempData works with .Net framework 1.0 and above. Need to do type conversion of code while enumerating. Session object keeps data for all requests. Valid for all requests, never expires.
通过这种方式,我们可以让它使用这些值来将控制器之间的信息传递到带有TEMP DATA的其他页面
viewdata:是一个字典,用于存储视图和控制器之间的数据,你需要将视图数据对象转换为视图中相应的模型,以便能够从中检索数据…
ViewBag:是一个动态属性,在它的工作类似于视图数据,但它是更好的,因为它不需要在视图中使用它之前被强制转换为相应的模型…