Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValidationMessageFor with model and html attribute only - MVC 3

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?

like image 998
Murali Murugesan Avatar asked Jan 09 '13 07:01

Murali Murugesan


People also ask

What is HTML ValidationMessageFor?

The Html. ValidationMessageFor() is a strongly typed extension method. It displays a validation message if an error exists for the specified field in the ModelStateDictionary object.

What are HTML helpers in MVC?

In MVC, HTML Helper can be considered as a method that returns you a string. This string can describe the specific type of detail of your requirement. Example: We can utilize the HTML Helpers to perform standard HTML tags, for example HTML<input>, and any <img> tags.


1 Answers

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

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

like image 147
Jared Avatar answered Nov 07 '22 19:11

Jared