Hyphenated html attributes with asp.net mvc

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"} }) %>

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.

E.g. HTML 5 proposes custom attributes are prefixed with data- and WIA ARIA uses the aria- prefix for all WIA ARIA attributes.

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.

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?

21505 次浏览

属性提供的答案建议使用下划线而不是连字符。MVC.Net 在向浏览器发送页面时应该发出连字符而不是下划线。

在 data 属性名称中使用下划线,它就会神奇地为您处理它,并将其转换为连字符。它知道您需要一个连字符而不是下划线,因为下划线在 html 属性名称中是无效的。

<%= Html.TextBox("name", value, new { @data_foo = "bar"}) %>