In my MVC3 application I have the model ( not important properties deleted ):
public class AccountViewModel
{
[StringLength(65)]
public string Property1 { get; set; }
[StringLength(65)]
public string Property2 { get; set; }
}
The problem is when an action is submited validation attribute called twice, and I can get 4 errors in summary, instead of 2:
'Property1' length must be less than 65 characters
'Property1' length must be less than 65 characters
'Property2' length must be less than 65 characters
'Property2' length must be less than 65 characters
I dont use Validate method in my controller's code. The problem appears also with my custom attributes, but its not happens with Required attribute. Also I have to note that ctor of the custom attributes also called twice
My action
[HttpPost]
public ActionResult CreateOrEdit(AccountViewModel model) {
if (!ModelState.IsValid) {
return View("Edit", model);
}
try {
_accountService.InsertOrUpdate(model);
}
catch (Exception ee) {
ModelState.AddModelError("", ee.Message);
return View("Edit", model);
}
return RedirectToAction("Index");
}
On View I render my errors using:
@{
var errors = ViewData.ModelState.Errors();
<div class="alert alert-block alert-error @(errors.Count == 0 ? "hide" : "")" >
<h4 class="alert-heading"> You got an error!</h4>
<ul>
@foreach (var error in errors) {
<li>@error</li>
}
</ul>
</div>
}
And I double re-check once more that ViewData.ModelState already contains errors twice.