How do you create html attributes with dashes when

2019-02-17 07:57发布

问题:

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

回答1:

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.