如何在MVC声明HTML辅助,当你创建一个破折号HTML属性? [英] How do you create html attributes with dashes when declaring html helpers in MVC?
问题描述
我如何,例如,创建一个数据绑定属性,当我宣布和Html.TextboxFor帮手?
How can I, for example, create a "data-bind" attribute when I declare and Html.TextboxFor helper?
做简单的:
@Html.TextBoxFor(model => model.SomeProperty, new { data-bind="something" })
是因为命名问题以短线不合法 - 符号。有没有解决这个问题的方法或只是不可能通过包含破折号名HTML属性?
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?
请注意:我试着拍打着@(这可以帮助,如果你想传递一个atrributed匹配,如阶级C#保留字)在属性的前面,但没有这样的伎俩......
NOTE: I tried slapping the @ (this helps if you want to pass an atrributed that matches C# reserved words like "class") in front of the attribute but that didn't do the trick...
推荐答案
您可以使用下划线( _
)的是,MVC将它们转换成短横线:
You can use underscores (_
) for that, MVC will convert them to dashes:
@Html.TextBoxFor(model => model.SomeProperty, new { data_bind = "something" })
注意 data_bind
属性。
这篇关于如何在MVC声明HTML辅助,当你创建一个破折号HTML属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!