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?
Asked
Active
Viewed 9,017 times
32

Matthew Verstraete
- 6,335
- 22
- 67
- 123
1 Answers
37
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.

Martin Beeby
- 4,523
- 1
- 26
- 20
-
Thanks this perfectly answers my question. I think I will be using `ViewData` my self. – Matthew Verstraete Jan 07 '16 at 02:44