问题描述
是否有创建具有连字符属性的元素时,而不是使用一个更好的语法:
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规范的建议标准HTML 5和似乎连字符的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提出自定义属性是prefixed与数据 -
和WIA ARIA使用咏叹调 -
preFIX所有WIA 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助手,如&LT;%= Html.TextBox(名,价值,新的{属性=的AttributeValue})%GT;
匿名对象转换为一个字典
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"}) %>
这篇关于联用HTML与asp.net的MVC属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!