MVC 3 override validation message css

狂风中的少年 提交于 2019-12-23 03:32:12

问题


I am trying to override the default validation css class for validators which is "field-validation-error" class

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

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

Then the MVC runtime is setting the class of the message span by default as below:

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

My question is, how can I remove the css class "field-validation-error" and replace it with another class (my version) which i will be using it in only some areas of my application.


回答1:


Ok...so I got it working by setting the base "field-validation-error" class as "form .field-validation-error" in the root css file...which means that this class will have the highest priority when the Html is generated and then in my View I included the new css class as mentioned below:

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

This will generate the below markup:

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

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

Cheers...




回答2:


Edit, you're right, I didn't read that far through the MVC source - just read the extension method source. Turns out those fields are static readonly with no options to change.

So, you're stuck with one of two choices:

  1. Deal with using the set class names.
  2. Write your own extension methods (or easier, just copy them from the MVC source and rename them).


来源:https://stackoverflow.com/questions/8733857/mvc-3-override-validation-message-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!