Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html helper takes a dictionary<string,object>, how to use this parameter?

If a html helper takes a idictionary as a parameter, how do I use it?

I tried:

<%= Html.Blah( new { id = "blah" }) %>

But that doesn't work.

like image 670
Blankman Avatar asked Jul 15 '10 15:07

Blankman


2 Answers

<%= Html.Blah( new Dictionary<string, object>(){
                                                   { "key", "value" },
                                                   { "key1", someObj },
                                                   { "blah", 1 }
                                               } );
like image 200
Justin Niessner Avatar answered Sep 21 '22 13:09

Justin Niessner


Just in case others run into this question too. There exists a helper method that will convert an anonymous object into a dictionary.

For example:

HtmlHelper.AnonymousObjectToHtmlAttributes( new { ng_model = "vm" } );

In other words, you can create your HTML helper method as per the OP's question to take an object and use this helper method inside. E.g.

public static string Blah(this HtmlHelper html, object htmlAttributes)
{
    // Create a dictionary from the object
    HtmlHelper.AnonymousObjectToHtmlAttributes( htmlAttributes );

    // ... rest of implementation
}
like image 21
Jaans Avatar answered Sep 22 '22 13:09

Jaans