title: Dynamically apply validation rules at runtime with ASP.NET MVC 4
body: <p>I've been working in WebForms for years but I'm fairly new to .NET's flavor of MVC. I am trying to figure out how to apply dynamic validation rules to members of my model at runtime. For purposes of this question these are simplified versions of the classes I'm working with:</p>
  
  <pre><code>public class Device
  {
     public int Id {get; set;}
     public ICollection&lt;Setting&gt; Settings {get; set;}
  }
  
  public class Setting
  {
     public int Id {get; set;} 
     public string Value {get; set;}
     public bool IsRequired {get; set;}
     public int MinLength {get; set;}
     public int MaxLength {get; set;}
  }
  </code></pre>
  
  <p>In my view I would iterate through the Settings collection with editors for each and apply the validation rules contained in each Setting instance at runtime to achieve the same client and server-side validation that that I get from using DataAnnotations on my model at compile-time. In WebForms I would have just attached the appropriate Validator to the associated field but I'm having trouble finding a similar mechanism in MVC4. Is there a way to achieve this?</p>