C# 如何在剃刀中指定数据属性,例如 @this.Html.CheckBoxFor(...) 上的 data-externalid="23151"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9444805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify data attributes in razor, e.g., data-externalid="23151" on @this.Html.CheckBoxFor(...)
提问by Ian Davis
@this.Html.CheckBoxFor(m => m.MyModel.MyBoolProperty, new { @class="myCheckBox", extraAttr="23521"})
With razor, I'm unable to specify values for data- attributes such as data-externalid="23521"
使用剃刀,我无法指定数据属性的值,例如 data-externalid="23521"
Is there a way to do this using @this.Html.CheckBoxFor(...)?
有没有办法做到这一点@this.Html.CheckBoxFor(...)?
采纳答案by Darin Dimitrov
@Html.CheckBoxFor(
m => m.MyModel.MyBoolProperty,
new {
@class = "myCheckBox",
data_externalid = "23521"
}
)
The _will automatically be converted to -in the resulting markup:
在_将自动转换到-在所得的标记:
<input type="checkbox" name="MyModel.MyBoolProperty" data-externalid="23521" class="myCheckBox" />
And that's true for all Html helpers taking a htmlAttributesanonymous object as argument, not only the CheckBoxForhelper.
对于所有将htmlAttributes匿名对象作为参数的Html 助手来说都是如此,而不仅仅是CheckBoxFor助手。

