32

I was searching for a way to pass ViewDataDictionary to a partial view in ASP.NET MVC that I came to this syntax:

new ViewDataDictionary { { "Name", "Value" } }

I'm a bit confused about the initializer syntax here. can anyone explain it to me?

1
  • 1
    MSDN on collection initializers in C# and VB Commented Nov 21, 2016 at 15:41

2 Answers 2

54

ViewDataDictionary implements IDictionary<string, object>.

IDictionary<string, object> is essentially a collection of KeyValuePair<string, object>.

Your ViewDataDictionary initializer (outer curly braces) contains another set of curly braces that represents a KeyValuePair<string, object> initializer.

The reason this is possible is explained in this answer.

You can Add multiple items by comma separating the KeyValuePair<string, object> initializers:

var data = new ViewDataDictionary 
{ 
    { "Name", "Value" }, 
    { "Name2", "Value2" } 
};

Is the same as:

var data = new ViewDataDictionary 
{ 
    new KeyValuePair<string, object>("Name", "Value"), 
    new KeyValuePair<string, object>("Name2", "Value2") 
};

Essentially, the inner curly braces are nice syntax for initializing KeyValuePair<string, object> objects.

Sign up to request clarification or add additional context in comments.

2 Comments

Are items in IDictionary<string, object> of type KeyValuePair<string, object>?
@Mahmoodvcs yes, IDictionary<string, object> is essentially a collection of KeyValuePair<string, object>.
4

I solved this using an extension method:

/// <summary>
/// Use this extension method to create a dictionary or objects
///     keyed by their property name from a given container object
/// </summary>
/// <param name="o">Anonymous name value pair object</param>
/// <returns></returns>
public static Dictionary<string, object> ToDictionary(this object o)
{
    var dictionary = new Dictionary<string, object>();

    foreach (var propertyInfo in o.GetType().GetProperties())
    {
        if (propertyInfo.GetIndexParameters().Length == 0)
        {
            dictionary.Add(propertyInfo.Name, propertyInfo.GetValue(o, null));
        }
    }

    return dictionary;
}

And an Html Helper extension:

/// <summary>
/// When viewData is null, we just return null.  Otherwise, we
///     convert the viewData collection to a ViewDataDictionary
/// </summary>
/// <param name="htmlHelper">HtmlHelper provided by view</param>
/// <param name="viewData">Anonymous view data object</param>
/// <returns></returns>
public static ViewDataDictionary vd(this HtmlHelper htmlHelper, object viewData)
{
    if (viewData == null) return null;

    IDictionary<string, object> dict = viewData.ToDictionary();

    //We build the ViewDataDictionary from scratch, because the
    //  object parameter constructor for ViewDataDictionary doesn't
    //  seem to work...
    ViewDataDictionary vd = new ViewDataDictionary();
    foreach (var item in dict)
    {
        vd[item.Key] = item.Value;
    }

    return vd;
}

Use from a razor file as:

@Html.Partial("~/Some/Path.cshtml", Model, Html.vd(new { SomeKey = SomeObj }))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.