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.
I am going through NerdDinner to understand how things work.
So, I have an object that needs to be passed through couple of views. Similar to the article NerdDinner Step 6: ViewData and ViewModel.
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.
Here's my 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
}
}