Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

example from msdn

// The class derived from DynamicObject. 
public class DynamicDictionary : DynamicObject
{
    // The inner dictionary.
    Dictionary<string, object> dictionary = new Dictionary<string, object>();

    public DynamicDictionary(Dictionary<string, object> d) { dictionary = d; }
    public DynamicDictionary() { }
    // This property returns the number of elements 
    // in the inner dictionary. 
    public int Count
    {
        get
        {
            return dictionary.Count;
        }
    }

    // If you try to get a value of a property  
    // not defined in the class, this method is called. 
    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        // Converting the property name to lowercase 
        // so that property names become case-insensitive. 
        string name = binder.Name.ToLower();

        // If the property name is found in a dictionary, 
        // set the result parameter to the property value and return true. 
        // Otherwise, return false. 
        return dictionary.TryGetValue(name, out result);
    }

    // If you try to set a value of a property that is 
    // not defined in the class, this method is called. 
    public override bool TrySetMember( SetMemberBinder binder, object value)
    {
        // Converting the property name to lowercase 
        // so that property names become case-insensitive.
        dictionary[binder.Name.ToLower()] = value;

        // You can always add a value to a dictionary, 
        // so this method always returns true. 
        return true;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Creating a dynamic dictionary.
        dynamic person = new DynamicDictionary(/*this can be your dictionary*/);

        // Adding new dynamic properties.  
        // The TrySetMember method is called.
        person.FirstName = "Ellen";
        person.LastName = "Adams";

        // Getting values of the dynamic properties. 
        // The TryGetMember method is called. 
        // Note that property names are case-insensitive.
        Console.WriteLine(person.firstname + " " + person.lastname);

        // Getting the value of the Count property. 
        // The TryGetMember is not called,  
        // because the property is defined in the class.
        Console.WriteLine( "Number of dynamic properties:" + person.Count);

        // The following statement throws an exception at run time. 
        // There is no "address" property,
        // so the TryGetMember method returns false and this causes a 
        // RuntimeBinderException. 
        // Console.WriteLine(person.address);
    }
}

// This example has the following output: 
// Ellen Adams 
// Number of dynamic properties: 2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top