Reputation: 53331
In a typical ASP.NET Web Pages page containing validation, there will be something like:
if (IsPost && Validation.IsValid()) {
// Process valid data here
}
Inside my HTML, I need to add various classes to certain elements for presentation purposes. Is it safe to call Validation.IsValid() there or will it trigger validation over and over again?
Edit: to make it clear, I'm asking about additional calls to IsValid, like this somewhere down the cshtml file:
<input type="text" class="forminput @(Validation.IsValid() ? "" : "error")" />
Upvotes: 0
Views: 104
Reputation: 30075
Calling Validation.IsValid()
without any arguments causes every form field to be examined for validation errors every time the method is called. If an entry has been added to the ModelState
dictionary on a previous call, it will still be there in subsequent calls - assuming that there hasn't been a Postback
in between calls, which will of course clear all state.
You can limit the number of fields that are checked by passing their names into the method:
if(Validation.IsValid("Email")){
// will only examine the form field named "Email"
}
Upvotes: 1