Hyphenated html attributes with asp.net mvc

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

Leave a Comment