Why not to use ViewBag heavily?

天大地大妈咪最大 提交于 2019-12-01 08:22:48

问题


What if i use too many ViewBag in MVC3 page? What extreme problems will be there if i continue use them heavily. In less words, what are the disadvantages of ViewBag and what is the alternate for it?


回答1:


what is the alternate for it?

You should only use ViewModels, It's the recommended approach. Create Strongly typed views. It would make your code cleaner and easy to maintain.

What extreme problems will be there?

Issue with Viewbags/ViewData is compile time checking. ViewBags are just dictionaries and you won't be aware about any issues until run time and thus it makes your code error-prone. So always try to go with ViewModels.




回答2:


Two points I know:

  1. It makes your controllers less testable.
  2. Your views are not strongly typed



回答3:


It has the possibility to hit performance at run-time.



来源:https://stackoverflow.com/questions/15567891/why-not-to-use-viewbag-heavily

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!