Last post Jun 02, 2010 07:11 AM by ivanlieckens
Jun 02, 2010 03:29 AM|mvcnoob1|LINK
MVC 1.0 source has code like
RouteValueDictionary attributes = htmlAttributes == null ? new RouteValueDictionary() : new RouteValueDictionary(htmlAttributes);
Was RouteValueDictionary intended for use with htmlAttributes? Is there an alternative? The only 'feature' RouteValueDictionary looks like its got is it has a constructor which accepts an object.
What should I be using in custom helpers?
Jun 02, 2010 07:07 AM|levib|LINK
Was RouteValueDictionary intended for use with htmlAttributes?
No. It's only intended use is for Routing APIs.
The only 'feature' RouteValueDictionary looks like its got is it has a constructor which accepts an object.
And this is what makes it useful for non-Routing scenarios, even if it was never intended for such. :) There's nothing inherently wrong with using RouteValueDictionary for this purpose, as it's just a glorified IDictionary<string, object>, and that's all
the MVC APIs care about. But even though MVC might use RouteValueDictionary internally, note that the MVC framework doesn't expose RouteValueDictionary anywhere in its public API surface unless that API is explicitly intended for Routing. It's recommended
that you follow this same pattern if you intend to distribute the binaries containing your custom helpers.
Jun 02, 2010 07:11 AM|IvanLieckens|LINK
Mostly you're using anonymous objects or IDictionary<string,object> for html attributes in html helpers. Even though RouteValueDictionary is such a Dictionary I don't find it appropriate to be used due to it's specific target: keeping routingvalues. So for
custom helper extension I'd use IDictionary<string, object> and to make it give the same look 'n feel as normal MVC extensions, make one with 'object' as well for anonymous objects in an overload. The tagbuilder knows very well how to work with these two.