How to specify data attributes in razor, e.g., data-externalid="23151" on @this.Html.CheckBoxFor(...)

@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"

Is there a way to do this using @this.Html.CheckBoxFor(...)?

70043 次浏览
@Html.CheckBoxFor(
m => m.MyModel.MyBoolProperty,
new {
@class = "myCheckBox",
data_externalid = "23521"
}
)

在产生的标记中,_将自动转换为 -:

<input type="checkbox" name="MyModel.MyBoolProperty" data-externalid="23521" class="myCheckBox" />

对于所有接受 htmlAttributes匿名对象作为参数的 Html 助手来说都是如此,而不仅仅是 CheckBoxFor助手。