2

Is there are a way to utilize Html.ValidationMessageFor() to return the validation text without the HTML markup around it?

Currently this code:

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

Returns this:

<span class="field-validation-error" data-valmsg-for="Model.SomeProperty" data-valmsg-replace="true">This field is required.</span>

And I would much prefer this:

This field is required.
tereško
  • 58,060
  • 25
  • 98
  • 150
Dan Waterbly
  • 850
  • 7
  • 15

2 Answers2

2

m0s's comment pointed me to this StackOverflow question/answer. (How do I get the collection of Model State Errors in ASP.NET MVC?)

This is probably redundant but here is my working solution:

@if (ViewData.ModelState.ContainsKey("SomeProperty))
{
     @Html.TextBoxFor(m => m.SomeProperty), 
          new {
               @some_attribute = ViewData.ModelState["SomeProperty"].Errors[0].ErrorMessage })     
}

Obviously, you want to ensure your ModelState has errors before addressing the first one, in my case this will always be true.

Community
  • 1
  • 1
Dan Waterbly
  • 850
  • 7
  • 15
0

Try: htmlHelper.ValidationMessage(string ModelName). I think that returns the string without the markup.

KennyZ
  • 907
  • 5
  • 11