NikolaiDante
NikolaiDante

Reputation: 18639

MVC Validation Message

On my view (aspx)

Html.ValidationMessage("FirstName") 

is giving a return of

<span class="field-validation-valid" data-valmsg-for="FirstName" data-valmsg-replace="true"></span>

Even though

ViewContext.ViewData.ModelState.Values

doesn't have an entry for FirstName in it.

If I call

Html.ValidationSummary()

Nothing is returned.

What is going on? Where should I look for these values?

This is on the initial load of my view, so there shouldn't be any validation mesages showing. How do I stop this unwanted message showing?. If I submit the page, then ViewContext.ViewData.ModelState.Values is populated how I would expect. Any pointers appreciated.

Upvotes: 1

Views: 2552

Answers (2)

David Spence
David Spence

Reputation: 8079

These data attributes are used to perform client side validation using jQuery. When you add an MVC validation attribute like Required or Range(0,2) the attributes are added automatically. The attributes being on your field doesn't make the field invalid. For an invalid state, the css class field-validation-valid class is removed and the css class field-validation-error is applied. To prevent the data attributes being generated, you can disable JavaScript which obviously has a knock on effect. I would leave as is and be safe in the knowledge that everything is fine :)

Upvotes: 0

Liam
Liam

Reputation: 29624

Thats correct functionality. The validation message has a class of class="field-validation-valid" the important bit is the Valid in this. If you invalidate the form you'll see the span will change to something like:

<span class="field-validation-error" data-valmsg-replace="true" data-valmsg-for="Destination.DestinationText">
<span class="" for="Destination_DestinationText" generated="true">The DestinationText field is required.</span>
</span>

notice the class="field-validation-error".

Upvotes: 2

Related Questions