我想将htmlAttributes作为object
传递给类似的方法......
foo.HtmlAttributes(new { data_bind = "foo"});
在所有MVC HtmlHelpers中,我使用下划线作为连字符,这将输出有效的html "data-bind"
根据以下问题,这是正在发生的事情:
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);
}
然后将调用Latter:
internal virtual void ApplyConfiguration(TagBuilder tag)
{
tag.MergeAttributes(this.Attributes);
}
然而,这会输出:
<div data_bind="foo"></div>
如何输出有效的HTML?
更新 感谢Zabavsky ...
public virtual void HtmlAttributes(object htmlAttributes)
{
this.Attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
}