在 MVC 中使用 ViewBag 不好吗? [英] Is using ViewBag in MVC bad?

查看:31
本文介绍了在 MVC 中使用 ViewBag 不好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 mvc 3 团队决定引入一个用于在控制器和视图之间进行动态数据交换的功能,称为 viewbag,但对于我们都知道的强类型视图来说,这是一件好事吗?与使用强类型视图相比,使用 ViewBag 有哪些积极和消极的方面?

It seem like mvc 3 team decided to bring in a feature for dynamic data exchange between a controller and a view called the viewbag but it is A good thing against the strongly typed view we all know about? What are some of the positive and negative aspects to using the ViewBag versus using a strongly typed view?

推荐答案

ViewBag 与之前的 ASP.NET MVC 1 和 2 中的 ViewData 是一回事.它恰好是动态的,而不需要像字典一样使用它带钥匙.我认为这根本不会取代强类型视图,实际上您应该尽可能少地使用 Viewdata/Viewbag.尽可能始终使用强类型视图,因为如果您的 Viewdata/Viewbag 中的名称发生变化,这将导致更少的错误,并通过不让 ViewData 在所有地方进行强制转换来使 HTML 更清晰.

The ViewBag is the same thing as ViewData in previous ASP.NET MVC 1 and 2. It just happens to be dynamic instead of needing to use it like a dictionary with keys. I don't think this will replace strongly typed views at all and in fact you should use Viewdata/Viewbag as little as possible. Always use strongly typed views whenever possible since it will lead to fewer errors if the names in your Viewdata/Viewbag change and make the HTML cleaner by not having ViewData casts all over the place.

这篇关于在 MVC 中使用 ViewBag 不好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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