Is ViewData the new standard over ViewBag for ASP.Net 5 (core)?

心已入冬 提交于 2019-12-03 08:15:00

问题


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?


回答1:


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:

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

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



来源:https://stackoverflow.com/questions/34635874/is-viewdata-the-new-standard-over-viewbag-for-asp-net-5-core

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