title: In an MVC4 controller action, how can I validate a model only when it's passed in?
body: <p>I have a class as such..</p>
  
  <pre><code>public class TestClass
  {
      [DataMember(IsRequired = true)]
      [Required]
      public string Test { get; set; }
      [DataMember(IsRequired = true)]
      [Required]
      public string Test2 { get; set; }
  }
  </code></pre>
  
  <p>I'm basically trying to achieve this with my controller action..</p>
  
  <pre><code>public ActionResult Index(string value1, string value2, TestClass testClass)
  {
      if (testClass != null &amp;&amp; !ModelState.IsValid)
      {
          //return validation errors
      }
      //continue processing
  </code></pre>
  
  <p>However due to MVC model binding, <code>TestClass</code> is not <code>null</code> and therefore it's instantiated containing <code>null</code> properties for the strings <code>Test</code> and <code>Test2</code>, which in turn forces it to check the <code>ModelState.IsValid</code> which is of course false.</p>
  
  <p>Can someone point out where I'm going wrong or suggest a better alternative?</p>
  
  <p>EDIT.</p>
  
  <p>I'm checking for a null because the requirement is that <code>value1</code> and <code>value2</code> have to be passed into the system, then the <code>TestClass</code> contains extra information on what is being supplied.  The extra information isn't required, however it is supplied it needs to be validated.</p>