How does the ASP.NET MVC's ViewBag
work? MSDN says it is just an Object
, which intrigues me, how does "Magic" properties such as ViewBag.Foo
and magic strings ViewBag["Hello"]
actually work?
Also, how can I make one and use it in my ASP.NET WebForms app?
Examples would be really appreciated!
Best Answer
ViewBag
is of typedynamic
but, is internally anSystem.Dynamic.ExpandoObject()
It is declared like this:
dynamic ViewBag = new System.Dynamic.ExpandoObject();
which is why you can do :
ViewBag.Foo = "Bar";
A Sample Expander Object Code: