我在MVC 3中看到了ViewBag。这和MVC 2中的ViewData有什么不同?
当前回答
Although you might not have a technical advantage to choosing one format over the other, you should be aware of some important differences between the two syntaxes. One obvious difference is that ViewBag works only when the key you’re accessing is a valid C# identifi er. For example, if you place a value in ViewData["Key With Spaces"], you can’t access that value using ViewBag because the code won’t compile. Another key issue to consider is that you cannot pass in dynamic values as parameters to extension methods. The C# compiler must know the real type of every parameter at compile time in order to choose the correct extension method. If any parameter is dynamic, compilation will fail. For example, this code will always fail: @Html.TextBox("name", ViewBag.Name). To work around this, either use ViewData["Name"] or cast the va
其他回答
所有的答案都表明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之间的一个主要区别是:
ViewData:它将返回object,不管你给它赋了什么,需要再次类型转换回原始类型。
ViewBag:它足够聪明,可以返回你分配给它的确切类型,不管你分配的是简单类型(即int,字符串等)还是复杂类型。
例如:控制器代码。
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
Products p1 = new Products();
p1.productId = 101;
p1.productName = "Phone";
Products p2 = new Products();
p2.productId = 102;
p2.productName = "laptop";
List<Products> products = new List<Products>();
products.Add(p1);
products.Add(p2);
ViewBag.Countries = products;
return View();
}
}
public class Products
{
public int productId { get; set; }
public string productName { get; set; }
}
}
视图代码。
<ul>
@foreach (WebApplication1.Controllers.Products item in ViewBag.Countries)
{
<li>@item.productId @item.productName</li>
}
</ul>
屏幕输出。
viewdata:是一个字典,用于存储视图和控制器之间的数据,你需要将视图数据对象转换为视图中相应的模型,以便能够从中检索数据…
ViewBag:是一个动态属性,在它的工作类似于视图数据,但它是更好的,因为它不需要在视图中使用它之前被强制转换为相应的模型…
Although you might not have a technical advantage to choosing one format over the other, you should be aware of some important differences between the two syntaxes. One obvious difference is that ViewBag works only when the key you’re accessing is a valid C# identifi er. For example, if you place a value in ViewData["Key With Spaces"], you can’t access that value using ViewBag because the code won’t compile. Another key issue to consider is that you cannot pass in dynamic values as parameters to extension methods. The C# compiler must know the real type of every parameter at compile time in order to choose the correct extension method. If any parameter is dynamic, compilation will fail. For example, this code will always fail: @Html.TextBox("name", ViewBag.Name). To work around this, either use ViewData["Name"] or cast the va
它使用c# 4.0动态特性。它实现了与viewdata相同的目标,应该避免使用强类型视图模型(与viewdata应该避免的方式相同)。
基本上它取代了魔法字符串:
ViewData["Foo"]
具有神奇属性:
ViewBag.Foo
你没有编译时安全性。
我继续指责微软在MVC中引入了这个概念。
属性的名称区分大小写。
推荐文章
- 按类型查找WPF窗口中的所有控件
- 数组与列表的性能
- 从Description属性中获取Enum
- 为什么使用try {} finally{}和一个空的try块?
- 如何在内存中获取对象大小?
- 每个优秀的。net开发人员都应该能够回答的问题?
- 如何编辑。csproj文件
- EscapeUriString和EscapeDataString的区别是什么?
- 在c++ /CLI中插入号(' ^ ')是什么意思?
- 什么时候应该使用TaskCompletionSource<T> ?
- 为什么处理排序数组比未排序数组慢?
- .net中ObservableCollection有什么用?
- LINQ单对第一
- 如何获得具有给定属性的属性列表?
- 例外。Message vs . Exception.ToString()