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

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

问题描述

如果我在 MVC3 页面中使用太多 ViewBag 怎么办?如果我继续大量使用它们会出现什么极端问题.简而言之,ViewBag 的缺点是什么,它的替代品是什么?

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?

您应该只使用 ViewModels,它是推荐的方法.创建强类型视图.这将使您的代码更简洁且易于维护.

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

会出现哪些极端问题?

Viewbags/ViewData 的问题是编译时检查.ViewBags 只是字典,直到运行时您才会意识到任何问题,因此它会使您的代码容易出错.所以总是尝试使用 ViewModel.

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天全站免登陆