0

when I'm using @Html.ValidationMessageFor helper on page for non-valid fields .Net generates error message in span-tag with standart class - "field-validation-error".

<span class="field-validation-error" data-valmsg-for="FirstName" data-valmsg-replace="true">The FirstName field is required.</span>

Can I set my custom class for it? I'm just trying to use Bootstrap 3 error alert classes. The FirstName field is required.

Alexander Smith
  • 369
  • 2
  • 16

1 Answers1

0

You can assign your own CSS class to be output for validation errors using

HtmlHelper.ValidationMessageCssClassName = "you class name here";

Note that you'll also need to modify jquery.validate.unobtrusive.js to use your class name if using jquery validate plugin on the client side.

Alternatively, you could copy the CSS rules from the class in question over to a .field-validation-error CSS class declaration.

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • So should I write my custom Helper? – Alexander Smith Sep 16 '14 at 14:32
  • You don't need to write your own custom helper if all you want to do is change the CSS class names (or define your own css class rule for the MVC CSS classes that matches the styling of Bootstrap). If you need to change the HTML emitted entirely, then you may want to write your own helper, yes. – Russ Cam Sep 16 '14 at 23:00