When writing an htmlhelper extension if I want to support the similarly structured ctors for my htmlhelper extension method, I use RouteValueDictionary as follows:
public static string ListBoxDict(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes) { return ListBoxDict(htmlHelper, name, value, ((IDictionary<string, object>) new RouteValueDictionary(htmlAttributes))); }
My question really is why the need for RouteValueDictionary … I know you can’t just cast the htmlAttributes to IDictionary<string, object> … though I’m not sure why and that might be where I’m confused. Shouldn’t RouteValueDictionary be to do with Routing and therefore nothing to do with HtmlHelper methods? Like I say, I’m probably missing the point so I’d be glad if someone could tell me what I’ve missed.
Cheers…
edit: in response to Dan’s answer –>
I was just following what I had seen in use in the mvc source code for input helpers…
- see ‘
src\SystemWebMvc\Mvc\Html\InputExtensions.cs‘
It does as follows:
public static string TextBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes) { return TextBox(htmlHelper, name, value, new RouteValueDictionary(htmlAttributes)) }
Clearly a shortcut but is it a bastardization or is it ok to do it?
I would strongly recommend looking at Rob Conery’s blog post about something like this.
The meat and veg of it is this:
Codedump:
Usage:
What
.ToAttributeList()does is convert your htmlAttribute object toHope this makes sense.