Marko
Marko

Reputation: 13273

How do you create html attributes with dashes when declaring html helpers in MVC?

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

Upvotes: 12

Views: 2748

Answers (1)

Henk Mollema
Henk Mollema

Reputation: 46661

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.

Upvotes: 20

Related Questions