title: ASP.NET MVC 3 Validation on Nested Objects not working as expected - validates child object twice and not parent object
body: <p>I am trying to get ASP.NET MVC 3 to generate forms from complex, nested objects.  There is one validation behaviour I found which was unexpected and I am not sure if it's a bug in the DefaultModelBinder or not.</p>
  
  <p>If I have two objects, lets call the "parent" one "OuterObject", and it has a property of type "InnerObject" (the child):</p>
  
  <pre><code>    public class OuterObject : IValidatableObject
  {
      [Required]
      public string OuterObjectName { get; set; }
  
      public InnerObject FirstInnerObject { get; set; }
  
      public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)
      {
          if (!string.IsNullOrWhiteSpace(OuterObjectName) &amp;&amp; string.Equals(OuterObjectName, "test", StringComparison.CurrentCultureIgnoreCase))
          {
              yield return new ValidationResult("OuterObjectName must not be 'test'", new[] { "OuterObjectName" });
          }
      }
  }
  </code></pre>
  
  <p>Here is InnerObject:</p>
  
  <pre><code>    public class InnerObject : IValidatableObject
  {
      [Required]
      public string InnerObjectName { get; set; }
  
      public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)
      {
          if (!string.IsNullOrWhiteSpace(InnerObjectName) &amp;&amp; string.Equals(InnerObjectName, "test", StringComparison.CurrentCultureIgnoreCase))
          {
              yield return new ValidationResult("InnerObjectName must not be 'test'", new[] { "InnerObjectName" });
          }
      }
  }
  </code></pre>
  
  <p>You will notice the validation I put on both.. just some dummy validation to say some value can't equal "test".</p>
  
  <p>Here is the view that this will display in (Index.cshtml):</p>
  
  <pre><code>@model MvcNestedObjectTest.Models.OuterObject
  @{
      ViewBag.Title = "Home Page";
  }
  
  @using (Html.BeginForm()) {
  &lt;div&gt;
      &lt;fieldset&gt;
          &lt;legend&gt;Using "For" Lambda&lt;/legend&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(m =&gt; m.OuterObjectName)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.TextBoxFor(m =&gt; m.OuterObjectName)
              @Html.ValidationMessageFor(m =&gt; m.OuterObjectName)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(m =&gt; m.FirstInnerObject.InnerObjectName)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.TextBoxFor(m =&gt; m.FirstInnerObject.InnerObjectName)
              @Html.ValidationMessageFor(m =&gt; m.FirstInnerObject.InnerObjectName)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Test Submit" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  &lt;/div&gt;
  }
  </code></pre>
  
  <p>..and finally here is the HomeController:</p>
  
  <pre><code>    public class HomeController : Controller
  {
      public ActionResult Index()
      {
          var model = new OuterObject();
          model.FirstInnerObject = new InnerObject();
          return View(model);
      }
  
      [HttpPost]
      public ActionResult Index(OuterObject model)
      {
          if (ModelState.IsValid)
          {
              return RedirectToAction("Index");
          }
          return View(model);
      }
  }
  </code></pre>
  
  <p>What you will find is that when the model gets validated by the DefaultModelBinder, the "Validate" method in "InnerObject" gets hit twice, but the "Validate" method in "OuterObject" does not get hit at all.</p>
  
  <p>If you take off IValidatableObject from "InnerObject", then the one on "OuterObject" will get hit.</p>
  
  <p>Is this a bug, or should I expect it to work that way?  If I should expect it to, what's the best workaround?</p>