什么是的ViewData和ViewBag之间的区别? [英] What's the difference between ViewData and ViewBag?

查看:878
本文介绍了什么是的ViewData和ViewBag之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到 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.

这篇关于什么是的ViewData和ViewBag之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆