I would like to pass htmlAttributes as an object to a method like this ...
foo.HtmlAttributes(new { data_bind = "foo"});
In all MVC HtmlHelpers I used the underscore as a hyphen, this displays a valid html "data-bind"
Under the hood, this is what happens on the following issues:
How to get values ββfrom an HtmlAttributes object
Passing an object into HTML attributes
public virtual void HtmlAttributes(object htmlAttributes) { this.Attributes = new RouteValueDictionary(htmlAttributes); }
And then Latter it will be called:
internal virtual void ApplyConfiguration(TagBuilder tag) { tag.MergeAttributes(this.Attributes); }
However, this will lead to the conclusion:
<div data_bind="foo"></div>
What can be done to produce valid HTML?
UPDATE Thanks to Zabavsky ...
public virtual void HtmlAttributes(object htmlAttributes) { this.Attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); }
source share