title: Validating lists in C#
body: <p>We're using <code>DataAnnotations</code> to validate our model.</p>
  
  <p>A very simplified version of our model is:</p>
  
  <pre><code>public class Model
  {
      public List&lt;Thing&gt; Things;
  }
  
  public class Thing
  {
      [Required]
      public string Name {get;set;}
  }
  </code></pre>
  
  <p>Now, the funny thing is that if I create a <code>Thing</code> with no name and add it to the model, I would expect validation to fail, but it passes (shock horror!).</p>
  
  <pre><code>var model = new Model ();
  var invalidThing = new Thing (); // No name would fail validation
  model.Things.Add(invalidThing );
  
  var validationContext = new ValidationContext(model);
  var validationResults = new List&lt;ValidationResult&gt;();
  var isValid = Validator.TryValidateObject(model, validationContext, validationResults, true);
  
  Assert.False (isValid);  // This fails!
  </code></pre>
  
  <p>I <em>think</em> the reason for this is that when you validate the model, it validates each property but not items in the property if it's a collection. <code>Things</code> is a property that has no validation, so it passes (despite the fact that it contains invalid item).</p>
  
  <p>How can we ensure that validation also validates items in collection properties? Is there some out-of-the-box validator I could use?</p>