如何在razor中指定数据属性,例如data-externalid=“23151";在@this.Html.CheckBoxFor(...) [英] How to specify data attributes in razor, e.g., data-externalid="23151" on @this.Html.CheckBoxFor(...)

查看:16
本文介绍了如何在razor中指定数据属性,例如data-externalid=“23151";在@this.Html.CheckBoxFor(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@this.Html.CheckBoxFor(m => m.MyModel.MyBoolProperty, new { @class="myCheckBox", extraAttr="23521"})

使用 razor,我无法指定数据属性的值,例如 data-externalid="23521"

With razor, I'm unable to specify values for data- attributes such as data-externalid="23521"

有没有办法使用 @this.Html.CheckBoxFor(...) 来做到这一点?

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

推荐答案

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

对于所有将 htmlAttributes 匿名对象作为参数的 Html 帮助器来说都是如此,而不仅仅是 CheckBoxFor 帮助器.

And that's true for all Html helpers taking a htmlAttributes anonymous object as argument, not only the CheckBoxFor helper.

这篇关于如何在razor中指定数据属性,例如data-externalid=“23151";在@this.Html.CheckBoxFor(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆