How to use @Html.Validationmessagefor to change the text box class

Asked

Viewed 1,852 times

4

I bought a bootstrap template, the validation of the controls is done by a class that leaves the controls red. This would be the classes form-group has-error

Only that I never used this way, I’m used to using the code generated by himself: Example:

@Html.EditorFor(model => model.Campo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Campo, "", new { @class = "text-danger" })

And then Validator writes the error message if the object comes back with error.

In the control I use the standard if (ModelState.IsValid)

How can I do to transform the class? If I can’t use the ValidationMessageFor, how could I do?

UPDATING:

I want to change the textbox class and not display a message.

1 answer

1

You can just add the class to your @html.ValidationMessageFor(), being like this:

@Html.EditorFor(model => model.Campo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Campo, "", new { @class = "form-group has-error" })

Look this example in . NET Fiddle.

In it I am adding the class error to my validation, leaving the source with 25px using that class:

.error{
    font-size: 25px;
}
  • So, it’s almost that! Only instead of appearing a message I would like it to just circle the textbox with a red line. (no need to have css for this, just want to know how to change the <div class="form-group">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.