使用 asp.net mvc 连字符的 html 属性
在创建具有连字符属性的元素而不是使用时,是否有更好的语法:
Is there a nicer syntax when creating elements with hyphenated attributes instead of using:
<%= Html.TextBox ("name", value, new Dictionary<string, object> { {"data-foo", "bar"} }) %>
查看拟议标准 HTML 5 的 HTML 规范和 WIA ARIA似乎 HTML 属性中的连字符被计划作为某种简单的名称间距更常见.
Looking at the HTML specs for the proposed standards HTML 5 and WIA ARIA it seems hyphens in HTML attributes are being planned to be more common as some sort of simple name spacing.
例如HTML 5 建议自定义属性以 data-
为前缀,WIA ARIA 对所有 WIA ARIA 属性使用 aria-
前缀.
E.g. HTML 5 proposes custom attributes are prefixed with data-
and WIA ARIA uses the aria-
prefix for all WIA ARIA attributes.
在 ASP.NET MVC 中使用 HTML helper 时,例如 <%= Html.TextBox("name", value, new { attribute = attributeValue }) %>
匿名对象被转换到字典.
When using HTML helpers in ASP.NET MVC such as <%= Html.TextBox("name", value, new { attribute = attributeValue }) %>
the anonymous object is converted to a dictionary.
不幸的是,在 C# 中不支持名称中的连字符,因此唯一的选择是创建一个字典.其语法非常冗长,有没有人看到更好的替代方法或简单的方法来改变 ASP.NET MVC 的 HTML 扩展的功能,而无需重新编写整个扩展?
Unfortunately in C# there is no support for hyphens in names, so the only alternative is to create a dictionary. The syntax for which is very verbose, has anyone seen a nicer alternative or a simple way of altering the functionality of ASP.NET MVC's HTML extensions without having to re-write the entire extension?
在数据属性名称中使用下划线,它会神奇地为您处理,将其转换为连字符.它知道您需要连字符而不是下划线,因为下划线在 html 属性名称中无效.
Use an underscore in the data attribute name, and it'll magically handle it for you, converting it to a hyphen. It knows you want a hyphen rather than an underscore as underscores aren't valid in html attribute names.
<%= Html.TextBox("name", value, new { @data_foo = "bar"}) %>