【发布时间】:2017-11-06 16:35:03
【问题描述】:
因此,bootstrap v4 alpha 对表单验证类进行了一些更改。现在,要将验证样式应用于表单输入,请将 CSS 类应用于父 div.form-group。
我正在使用 ASP.NET MVC4 编写一个网站,并试图弄清楚如何将此 CSS 类应用于父 HTML 元素。
例如,这是我当前用于表单输入元素的 HTML ...
<div class="form-group">
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Password)
</div>
如果我的视图模型的 Password 字段存在验证错误,它将在输入字段下方获得相应的文本位。这就是ValidationMessageFor 调用的作用。
但是,使用 bootstrap v4,我需要将 has-danger 类应用于父 div.form-group。它需要看起来像这样......
<div class="form-group has-danger">
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Password)
</div>
但是,我只想在密码字段有验证消息时应用它。
知道如何使用 Razor 实现这一目标吗?
【问题讨论】:
标签: asp.net-mvc twitter-bootstrap razor