It seems like ViewBag.SomeKey
works a lot like a php array in the sense that it seems to offer no compiletime checking of key names. I am wondering if there is a one-to-one correspondence with ViewBag
and some dictionary class with extra methods, ie if ViewBag.SomeKey
works in the same manner as myDictionary["SomeKey"]
.
Also, I am wondering how I might convert a dictionary into a dynamic object.
ViewBag
is a dynamic wrapper around ViewData
, which is a dictionary (ViewDataDictionary
). Writing ViewBag.SomeKey
is the same as ViewData["SomeKey"]
You can initialize it like this :
foreach(var item in myDictionary)
{
ViewData[item.Key] = item.Value;
}
Each item will be available as ViewData["Key"]
or ViewBag.Key
.