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.
<%= Html.Blah( new Dictionary<string, object>(){
{ "key", "value" },
{ "key1", someObj },
{ "blah", 1 }
} );
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With