3

I'm trying to style my @HTML.ValidationMessageFor element with Bootstrap 3 but no CSS is applied. When consulting Bootstrap's documentation, it states:

To use, add .has-warning, .has-error, or .has-success to the parent element. Any .control-label, .form-control, and .help-block within that element will receive the validation styles.

Even after nesting the desired @Html.ValidationMessageFor as stated above, no styling is applied:

ValidationMessageForError

Please see my code snippets below:

Model Property:

[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; }

View:

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

EDIT WITH ANSWER:

<div class="has-error">
    @Html.ValidationMessageFor(m => m.searchClientName, String.Empty, new { @class = "help-block" })
</div>
2
  • has-error class should be for form-group element Commented Feb 24, 2016 at 13:58
  • @PavanTeja I've tried replacing the div's class from form-group to has-error and it didn't change the styling at all. I tried applying the has-error class to both the div and the razor markup but it had no difference. Commented Feb 24, 2016 at 14:13

1 Answer 1

5

dont replace form-group with has-error.I have used something similar to this recently and try the following markup.

            <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>
Sign up to request clarification or add additional context in comments.

1 Comment

I left the has-error class for the div element and added the help-block class to the @Html.ValidationMessageFor HTML helper. This worked perfectly! Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.