8

How can I, for example, create a data-bind attribute when I declare and Html.TextboxFor helper?

Doing simply:

@Html.TextBoxFor(model => model.SomeProperty, new { data-bind="something" })

is not legitimate because of the naming issue with a dash "-" symbol. Is there a way around this issue or is it just not possible to pass html attributes with names containing dashes?

NOTE: I tried slapping the @ (this helps if you want to pass an atrribute that matches C# reserved words like "class") in front of the attribute but that didn't do the trick...

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
Marko
  • 12,543
  • 10
  • 48
  • 58
  • So underscore is converted to "-" within the html helper? – Marko Nov 01 '13 at 15:45
  • 2
    Yes. Duplicate of [Html5 data-* with asp.net mvc TextboxFor html attributes](http://stackoverflow.com/questions/4844001/html5-data-with-asp-net-mvc-textboxfor-html-attributes) or [Hyphenated html attributes with asp.net mvc](http://stackoverflow.com/questions/2897733/hyphenated-html-attributes-with-asp-net-mvc). – Zabavsky Nov 01 '13 at 15:47
  • another duplicate: http://stackoverflow.com/questions/12759396/dropdownlistfor-with-a-custom-attribute-with-in-attribute-name/12759522#12759522 – dove Nov 01 '13 at 15:50
  • that's a good answer @Zabavsky but it's a bad comment ;) – dove Nov 01 '13 at 15:52
  • your first one. don't get me wrong, it's very helpful and it is correct. the site philosophy though would like this question to be politely closed and redirected to one of the existing duplicates as it doesn't really add much to the canon of razor knowledge... – dove Nov 01 '13 at 15:56
  • @dove, you are right. I removed the comment and flagged the question as duplicate. – Zabavsky Nov 01 '13 at 15:57
  • @Zabavsky thanks, it's pedantic but it's how to keep the site quality. I +1'd your other comment ;) – dove Nov 01 '13 at 16:02

1 Answers1

15

You can use underscores (_) for that, MVC will convert them to dashes:

@Html.TextBoxFor(model => model.SomeProperty, new { data_bind = "something" })

Notice the data_bind property.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104