title: MVC ViewData Model null after Post-back to Controller Action
body: <p>I have a simple <strong>MVC 4 (Beta)</strong> Application and just observed something that I assume may be handled differently. In this case, when the below Product Action is called a Model is created and assigned to <strong>ViewData</strong> for use in the <strong>View</strong>. This all works fine, the <strong>Model</strong> property is not-null, and the Views use of the Model property works.</p>
  
  <pre><code>[HttpGet]
  public ActionResult Product()
  {
      return ExecuteFormRequest(View(), true, delegate
      {
          var model = Request.ParseFromQueryString&lt;Product&gt;();
  
          if (model != null)
          {
              ViewData.Model = model;
          }
  
          return View();
      });
  }
  </code></pre>
  
  <p>When the Product is updated and Post'ed back to the Controller all the data looks fine, but if the product fails validation and I return the View ActionResult (<em>as shown below</em>) the <strong>View</strong> encounters a null reference exception because the Views <strong>Model</strong> property is now null </p>
  
  <pre><code>[HttpPost]
  public ActionResult Product(Product product)
  {
      if (!ModelState.IsValid)
      {
          return View();
      }
  
      // Do other business stuff and target new or same View    
  
      return View();
  }
  </code></pre>
  
  <p>This is the line in the <strong>View</strong> that leads to a null reference exception, because the <strong>Model</strong> Property is now null. </p>
  
  <pre><code>@if (!string.IsNullOrEmpty(Model.MyValue)) {}
  </code></pre>
  
  <p>The way I resolved this was to reset the <strong>Model</strong> Property (<em>as shown below</em>). This seems wrong to me and I'm wondering if I'm approaching this all wrong. Any ideas welcome?</p>
  
  <pre><code>[HttpPost]
  public ActionResult Product(Product product)
  {
      if (product!= null)
      {
          ViewData.Model = product;
      }
  
      if (!ModelState.IsValid)
      {
          return View();
      }
  
      // Do other business stuff and target new or same View    
  
      return View();
  }
  </code></pre>