I\'m messing around with data annotations. When I click on a link to go to a page, the validation messages are being displayed, but I would like to have the validation messa
Set the validation style to:
.validation-summary-valid { display:none; }
So by default it's hidden. An error will trigger it to display.
.field-validation-valid {
display: none;
}
Whenever the validation triggers on page load, this ".field-validation-valid" value is automatically added to the class attribute of the triggered input element.
By adding CSS to display none as that particular class's value, you'll no longer see the validation messages on initial page load.
The validation messages will still display normally after the particular input element has been touched.
You can clear model state after binding user:
ModelState.Clear();
This happens because ModelBinder
will set ModelState
on binding.
In every action that binds a model and returns a view with the same model you will have this problem.
[HttpPost]
public ActionResult AddUser(UserCreateViewModel user)
{
if (ModelState.IsValid)
{
var success = UserRepository.AddUser(user);
if (success)
{
return View("Success");
}
}
ModelState.Clear(); // <-------
return View("AddUser");
}
$('.field-validation-error').html("");