title: Passing Data from Controller to View, back to Controller, back to View
body: <p>I'm new to ASP.NET MVC. After working with traditional ASP.NET model for so long, it's taking some time for me to get to understand this model. </p>
  
  <p>I am going through NerdDinner to understand how things work. </p>
  
  <p>So, I have an object that needs to be passed through couple of views. Similar to the article <a href="http://nerddinnerbook.s3.amazonaws.com/Part6.htm" rel="nofollow noreferrer">NerdDinner Step 6: ViewData and ViewModel</a>.</p>
  
  <p>I retain the data from Get to Post for the first time, then I put it in TempData and pass it to another action (AnotherAction). Once I get my data on Get I cannot retain it on Post.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public class DinnerFormViewModel
  {
      public Dinner Dinner { get; private set; }
  
      public DinnerFormViewModel(Dinner dinner)
      {
          Dinner = dinner;
      }
  }
  
  public class DinnersController : Controller
  {
      public ActionResult Action()
      {
          Dinner dinner = new Dinner();
          return View(new DinnerFormViewModel(dinner));
      }
  
      [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult Action(Dinner dinner, FormCollection collection)
      {
          try
          {
              // Some code
              TempData["Dinner"] = dinner;
              return RedirectToAction("AnotherAction");
          }
          catch
          {
              return View();
          }
      }
  
      public ActionResult AnotherAction()
      {
          Dinner dinner = (Dinner)TempData["Dinner"]; // Got my dinner object
          return View(new DinnerFormViewModel(dinner));
      }
  
      [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult AnotherAction(Dinner dinner, FormCollection collection)
      {
          // Lost my dinner object, dinner comes in as null
      }
  }
  </code></pre>