Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation Attribute get triggered two times
    primarykey
    data
    text
    <p>In my MVC3 application I have the model ( not important properties deleted ):</p> <pre><code>public class AccountViewModel { [StringLength(65)] public string Property1 { get; set; } [StringLength(65)] public string Property2 { get; set; } } </code></pre> <p>The problem is when an action is submited validation attribute called twice, and I can get 4 errors in summary, instead of 2:</p> <pre><code>'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 </code></pre> <p>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</p> <p>My action</p> <pre><code>[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"); } </code></pre> <p>On View I render my errors using:</p> <pre><code>@{ var errors = ViewData.ModelState.Errors(); &lt;div class="alert alert-block alert-error @(errors.Count == 0 ? "hide" : "")" &gt; &lt;h4 class="alert-heading"&gt; You got an error!&lt;/h4&gt; &lt;ul&gt; @foreach (var error in errors) { &lt;li&gt;@error&lt;/li&gt; } &lt;/ul&gt; &lt;/div&gt; } </code></pre> <p>And I double re-check once more that ViewData.ModelState already contains errors twice.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload