asp.net-mvc Adding a custom attribute with - (hyphen) in name


Example

If you need to add an attribute through razor that has a - (hyphen) in the name you cannot simply do

@Html.DropDownListFor(m => m.Id, Model.Values, new { @data-placeholder = "whatever" })

it will not compile. data-* attributes are valid and common in html5 for adding extra values to elements.

However the following will work

@Html.DropDownListFor(m => m.Id, Model.Values, new { @data_placeholder = "whatever" })

since "_" is replaced with "-" when rendered.

This works fine as underscores are not acceptable in attribute names in html.