使用 Bootstrap3 设置 Html.ValidationMessageFor 样式

Sea*_*ean 3 html css razor twitter-bootstrap asp.net-mvc-5

我正在尝试@HTML.ValidationMessageFor使用 Bootstrap 3 设置元素的样式,但没有应用 CSS。在查阅Bootstrap 的文档时,它指出:

要使用,请将 .has-warning、.has-error 或 .has-success 添加到父元素。该元素中的任何 .control-label、.form-control 和 .help-block 都将接收验证样式。

@Html.ValidationMessageFor即使在如上所述嵌套所需的样式后,也不会应用任何样式:

错误验证消息

请参阅下面我的代码片段:

模型属性:

[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; }
Run Code Online (Sandbox Code Playgroud)

看法:

<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>
Run Code Online (Sandbox Code Playgroud)

编辑答案:

<div class="has-error">
    @Html.ValidationMessageFor(m => m.searchClientName, String.Empty, new { @class = "help-block" })
</div>
Run Code Online (Sandbox Code Playgroud)

Pav*_*eja 5

不要替换form-grouphas-error。我最近使用了与此类似的东西并尝试以下标记。

            <div class="form-group has-error">
                <div >
                    @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
                </div>
                @Html.LabelFor(m => m.ConfirmPassword, new { @class = "control-label" })
                @Html.ValidationMessageFor(m => m.ConfirmPassword, "", new { @class = "help-block" })


            </div>
Run Code Online (Sandbox Code Playgroud)