ViewData是ViewBag上ASP.Net 5(核心)的新标准吗? [英] Is ViewData the new standard over ViewBag for ASP.Net 5 (core)?

查看:54
本文介绍了ViewData是ViewBag上ASP.Net 5(核心)的新标准吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注意到,在我从Microsoft看到的所有ASP.Net 5(核心)示例和教程中,VS 2015中的默认Web应用程序模板都使用@ViewData["XXX"]而不是@ViewBag.XXX.这是现在推荐的从控制器而不是ViewBag传递数据的方法吗?我知道ViewBagViewData的包装,但是在旧教程(ASP.NET 4.5)中,他们使用ViewBag.如果他们现在鼓励开发人员使用ViewData为什么要进行更改?

I have noticed that in all the examples and tutorials on ASP.Net 5 (core) I have seen from Microsoft and the default Web Application template in VS 2015 use @ViewData["XXX"] instead of @ViewBag.XXX. Is this the, now, recommended way of passing data up from the controller instead of ViewBag? I know that ViewBag is a wrapper for ViewData but in the old tutorials (ASP.NET 4.5) they use ViewBag. If they are now encouraging developers to use ViewData why the change?

推荐答案

两者仍然有效. docs.asp.net github项目.尽管在docs.asp.net问题上有此讨论.

Both are still valid. There is no specific guidance on the docs.asp.net github project. Although there is this discussion on docs.asp.net issues.

该链接链接到产品团队的一个评论,内容为:

That links through to a comment from one the product team which says:

因为ViewData(字典)查询的性能远远超过ViewBag (动态)调用,最后一个可能是最好的选择."

"Since ViewData (Dictionary) look-ups far out-perform ViewBag (dynamic) invocations, the last is probably the best choice."

因此,我纯粹基于ViewData性能更好的事实来说这是一种样式选择.

So I'd say it purely a style choice based upon the fact that ViewData performs better.

这篇关于ViewData是ViewBag上ASP.Net 5(核心)的新标准吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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