18

Working on a MVC 3 application.

I need to add a class to the validation message html. I mean for span tag.

My Model

 [Required(ErrorMessage = "Role Name is required")]
 [RegularExpression(@"^[a-zA-Z ]+$", 
     ErrorMessage = "Only alphabets and spaces allowed")]
 public string RoleName { get; set; }

Markup

  <label for="name">RoleName</label>
  @Html.TextBoxFor(m => m.RoleName)
  @Html.ValidationMessageFor(m => m.RoleName, "some", 
                           new { @class = "immediate" })

But i could not see overloaded method like @Html.ValidationMessageFor(m => m.RoleName, htmlAttributes) . if i give some text, it is not showing my validaton message entered in model. It always shows that some text only.

Any fix for this?

Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120

1 Answers1

25

You should be able to simply pass in null...

@Html.ValidationMessageFor(m => m.RoleName, null, new { @class = "immediate"})

Jared
  • 5,840
  • 5
  • 49
  • 83
  • i had a null missing, tx! – ptim Jul 15 '14 at 13:34
  • 1
    Neither null nor empty string for the second argument has the effect of failing over to the error message in the model - i seem to get an auto-generated message from the framework. Anything else prerequisite to getting this to work? – Brian Sweeney Mar 18 '19 at 17:26