title: How to validate only part of the model in ASP .NET MVC?
body: <p>I have a large model (large I mean model class contains a lot of fields/properties and each has at least one validation attribute (such as <code>Required</code>, <code>MaxLength</code>, <code>MinLength</code> etc)). Instead of creating one view with a lot of inputs for user to fill model with data I want to create several views where user will fill part of model fields and go to the next step (some kind of <em>"wizard"</em>). While redirecting between steps I store not fullfilled model object in <code>Session</code>. Something like below:</p>
  
  <p>Model:</p>
  
  <pre><code>public class ModelClass
  {
      [MaxLength(100)] ...
      public string Prop1{get;set;}
      [MaxLength(100)] ...
      public string Prop2{get;set;}
      ...
      [Required][MaxLength(100)] ...
      public string Prop20{get;set;}
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Step1(ModelClass postedModel)
  {    
      // user posts only for example Prop1 and Prop2
      // so while submit I have completly emty model object
      // but with filled Prop1 and Prop2
      // I pass those two values to Session["model"]
      var originalModel = Session["model"] as ModelClass ?? new ModelClass();
      originalModel.Prop1 = postedModel.Prop1;
      originalModel.Prop2 = postedModel.Prop2;
      Session["model"] = originalModel;
  
      // and return next step view
      return View("Step2");
  }
  
  [HttpPost]
  public ActionResult Step2(ModelClass postedModel)
  {
      // Analogically the same
      // I have posted only Prop3 and Prop4
  
      var originalModel = Session["model"] as ModelClass;
      if (originalModel!=null)
      {
          originalModel.Prop3 = postedModel.Prop3;
          originalModel.Prop4 = postedModel.Prop4;
          Session["model"] = originalModel;
  
          // return next step view
          return View("Step3");
      }
      return View("SomeErrorViewIfSessionBrokesSomeHow")
  }
  </code></pre>
  
  <p><code>Step1</code> view has inputs only for <code>Prop1</code> and <code>Prop2</code>, Step2 view contains inputs for <code>Prop3</code> and <code>Prop4</code> etc. </p>
  
  <p><strong>BUT HERE IS THE THING</strong> </p>
  
  <p>When user is on, for example, step 1, and fills Prop1 with value more than 100 characters length client side validation works fine. But, of course , I have to validate this value and on the server side in controller. If I had full model I'd just do the following:</p>
  
  <pre><code>if(!ModelState.IsValid) return View("the same view with the same model object");
  </code></pre>
  
  <p>so user has to fill the form again and correct.
  <strong>BUT</strong> on step 1 user has filled only 2 properties of 20, and I need to validate them. I can't use <code>ModelState.IsValid</code> because model state will be invalid. As You can see <code>Prop20</code> is marked with <code>[Required]</code> attribute, when user submits <code>Prop1</code> and <code>Prop2</code>, <code>Prop20</code> is null and that's why <code>ModelState</code> is invalid. Of course I could allow user to go to step2, fill all of the steps and validate model state only on the last step but I don't want to allow user to go to step 2 if he filled step 1 incorrect. And I want this validation in controller. <em>So the question is:</em>
  <strong>How can I validate only part of the model? How can I verify that only some of the model properties match their validation attributes?</strong></p>