【发布时间】:2016-06-06 18:42:56
【问题描述】:
我正在尝试使用 Bootstrap 3 设置 @HTML.ValidationMessageFor 元素的样式,但没有应用 CSS。在咨询Bootstrap's documentation时,它表示:
要使用,请将 .has-warning、.has-error 或 .has-success 添加到父元素。该元素中的任何 .control-label、.form-control 和 .help-block 都将接收验证样式。
即使如上所述嵌套了所需的@Html.ValidationMessageFor,也不会应用任何样式:
请看下面我的代码sn-ps:
模型属性:
[Range(0, int.MaxValue, ErrorMessage = "Client ID cannot be larger than 2147483647.")]
[RegularExpression(@"^[0-9]+$", ErrorMessage = "Client ID must be a positive number.")]
public int searchClientID { get; set; }
查看:
<div class="form-group col-sm-4">
<div class="input-group">
<span class="input-group-addon" id="client-ID">Client ID</span>
@Html.TextBoxFor(m => m.searchClientID, new { @class = "form-control" })
</div>
<div class="has-error">
@Html.ValidationMessageFor(m => m.searchClientID, null, new { @class = "has-error" })
</div>
</div>
编辑答案:
<div class="has-error">
@Html.ValidationMessageFor(m => m.searchClientName, String.Empty, new { @class = "help-block" })
</div>
【问题讨论】:
-
has-error 类应该用于 form-group 元素
-
@PavanTeja 我尝试将 div 的类从 form-group 替换为 has-error 并且它根本没有改变样式.我尝试将
has-error类应用于 div 和 razor 标记,但没有区别。
标签: html css twitter-bootstrap razor asp.net-mvc-5