12

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...

7
  • So underscore is converted to "-" within the html helper? Commented Nov 1, 2013 at 15:45
  • 2
    Yes. Duplicate of Html5 data-* with asp.net mvc TextboxFor html attributes or Hyphenated html attributes with asp.net mvc. Commented Nov 1, 2013 at 15:47
  • another duplicate: stackoverflow.com/questions/12759396/… Commented Nov 1, 2013 at 15:50
  • that's a good answer @Zabavsky but it's a bad comment ;) Commented Nov 1, 2013 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... Commented Nov 1, 2013 at 15:56

1 Answer 1

20

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.