Your question has a very simple answer:
ViewBag is a dynamic wrapper for ViewData . They are actually one and the same. Everything that you put in ViewData is also in ViewBag and vice versa.
You can see the implementation of DynamicViewDataDictionary (this is what the ViewBag is an instance of) here . As you can see, it simply contains an internal reference to the ViewDataDictionary , which it wraps and delegates access to the properties of dictionary elements.
So, when the environment passes the ViewData to the template with a specific key, and you assign the ViewBag property with the appropriate name, only one can win.
The other side of the coin, which, in my opinion, is more relevant, is that the static ModelMetadata methods that are responsible for checking ViewData metadata, first - before considering the metadata of the model object.
So, if you assign ViewBag properties (and - by association - ViewData ), these are properties that will be mapped to the expression you passed to the template and they will not have specific metadata, such as those provided by data annotations for the model class.
Ant p source share