ViewData 和 ViewBag 有什么区别? [英] What's the difference between ViewData and ViewBag?
本文介绍了ViewData 和 ViewBag 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 MVC 3 中看到了 ViewBag
.它与 MVC 2 中的 ViewData
有什么不同?
I saw the ViewBag
in MVC 3. How's that different than ViewData
in MVC 2?
推荐答案
它使用 C# 4.0 动态特性.它实现了与 viewdata 相同的目标,应该避免使用强类型视图模型(与应该避免使用 viewdata 的方式相同).
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).
所以基本上它取代了魔法字符串:
ViewData["Foo"]
具有魔法属性:
ViewBag.Foo
您没有编译时安全性.
我一直责怪微软在 MVC 中引入了这个概念.
I continue to blame Microsoft for ever introducing this concept in MVC.
属性名称区分大小写.
这篇关于ViewData 和 ViewBag 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文