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

How can I create, for example, the data-bind attribute on declaration and the Html.TextboxFor helper?

The execution is simple:

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

is not legal due to a problem with names with a dash "-". Is there a way around this problem or is it just not possible to pass html attributes with names containing dashes?

NOTE. I tried to tickle @ (this helps if you want to pass an attribute that matches C # to reserved words like "class") before the attribute, but that didn't help ...

+6
source share
1 answer

You can use underscores ( _ ) for this, MVC converts them to dashes:

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

Pay attention to the data_bind property.

+9
source

Source: https://habr.com/ru/post/957198/


All Articles