MVC 3 css override check message

I am trying to override the default authentication css class for validators, which is the field validation class

So, if I write a Validation message for input in my form, as shown below:

@Html.ValidationMessageFor(m=>m.Name) 

The MVC runtime then sets the default message range class, as shown below:

 <span class="field-validation-error" data-valmsg-replace="true" data-valmsg-for="Name"> 

My question is how to remove the css class "error-validation-error" and replace it with another class (my version), which I will use in only certain areas of my application.

+1
source share
2 answers

Ok ... so I got it by setting the base class "validation of the field" as "form-validation-error" in the root css file ... which means that this class will have the highest priority when creating Html and then in In my view, I included a new css class as follows:

 @Html.ValidationMessageFor(m=>m.Name, new { @class ="NewCssClass"}) 

This will result in the following markup:

 <span class="field-validation-error NewCssClass" data-valmsg-replace="true" data-valmsg-for="Name"> 

As you can see, NewCssClass will override the default validation style specified in the field validation class ...

Greeting...

+3
source

Change, you're right, I have not read this far from the source of MVC - just read the source of the extension method. It turns out these fields are static readonly without any parameters to change.

So, you are stuck in one of two options:

  • Refer to the use of class class names.
  • Write your own extension methods (or easier, just copy them from the MVC source and rename them).
0
source

Source: https://habr.com/ru/post/911679/


All Articles