为什么不使用ViewBag严重? [英] Why not to use ViewBag heavily?

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

问题描述

如果我用MVC3页面太多ViewBag什么?什么极端的问题会在那里,如果我继续大量使用它们。在少言,什么是ViewBag的缺点,什么是替代呢?


解决方案

  

什么是它的备用?


您应该只使用<一个href=\"http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3fundamentals_topic7.aspx\">ViewModels,这是推荐的方法。创建强类型的意见。它将使你的code更清洁,易于维护。


  

什么极端的问题,将在那里?


问题与Viewbags / ViewData的是编译时检查。 ViewBags只是字典,你不会知道,直到左右运行时的任何问题,因此它使你的code容易出错。所以,总是试图去用的ViewModels。

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?

解决方案

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.

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

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