Reputation: 6111
I would like to pass htmlAttributes as an object
to a method like so...
foo.HtmlAttributes(new { data_bind = "foo"});
In all the MVC HtmlHelpers I have used the underscore as a hyphen, this would output valid html "data-bind"
Under the hood this is what is going on as per the following questions:
How to get values out of object HtmlAttributes
Passing an object to HTML attributes
public virtual void HtmlAttributes(object htmlAttributes)
{
this.Attributes = new RouteValueDictionary(htmlAttributes);
}
And then Latter this will be called:
internal virtual void ApplyConfiguration(TagBuilder tag)
{
tag.MergeAttributes(this.Attributes);
}
However this would output:
<div data_bind="foo"></div>
What can I do to output valid HTML?
UPDATE Thanks to Zabavsky...
public virtual void HtmlAttributes(object htmlAttributes)
{
this.Attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
}
Upvotes: 4
Views: 1529
Reputation: 13640
HtmlHelper
class has AnonymousObjectToHtmlAttributes method, which helps to create markup that is compliant with HTML5. The method replaces underscore characters with hyphens.
Upvotes: 6