什么是的ViewData和ViewBag之间的区别?是的、区别、ViewData、ViewBag

2023-09-02 01:21:00 作者:酒醉人不归

我看到 ViewBag 在MVC 3怎么样,在MVC 2不同于的ViewData ?

I saw the ViewBag in MVC 3. How's that different than ViewData in MVC 2?

推荐答案

它使用C#4.0的动态特性。它实现了相同的目标作为可视数据,应该避免倾向于使用强类型视图模型(相同的方式,应避免可视数据)的

It uses the C# 4.0 dynamic feature. It achieves the same goal as viewdata and should be avoided in favor of using strongly typed view models (the same way as viewdata should be avoided).

所以基本上它取代的魔弦的:

So basically it replaces magic strings:

ViewData["Foo"]

与魔法属性的:

ViewBag.Foo

有哪些你没有编译时的安全性。

for which you have no compile time safety.

我继续指责微软曾经推出这个概念在MVC。

I continue to blame Microsoft for ever introducing this concept in MVC.

属性的名称是区分大小写的。

The name of the properties are case sensitive.