2

when using HtmlHelpers in Razor code, like this:

new { data-something_something = "value" }

The underscores are converted "magically" to hyphens. But what if I need my attributes to contain underscores AND hyphens? How can I escape or otherwise preserve the underscore?

Timothy Kanski
  • 1,861
  • 14
  • 20

1 Answers1

2

You can pass an IDictinoary<string, string> instead:

@Html.TextBoxFor(x => x.Prop, new Dictionary<string, string> { { "data-something_something", "value" } })

Please note that attribute names like some_attr is considered invalid HTML, but in your case data-* attributes can indeed contain underscores.

See MSDN

haim770
  • 48,394
  • 7
  • 105
  • 133