5

How would you pass the model from an (GetDate) action to another (ProcessP) action via RedirectAction method?

Here's the source code:

[HttpPost]
public ActionResult GetDate(FormCollection values, DateParameter newDateParameter)
{
    if (ModelState.IsValid)
    {
return RedirectToAction("ProcessP");
    }
    else
    {
return View(newDateParameter);
    }
}


public ActionResult ProcessP()
{
   //Access the model from GetDate here??
    var model = (from p in _db.blah
 orderby p.CreateDate descending
 select p).Take(10);

    return View(model);
}
scv
  • 323
  • 10
  • 20

1 Answers1

8

If you need to pass data from one action to another one option is to utilize TempData. For example within GetDate you could add data to the session as follows:

TempData["Key"] = YourData

And then perform the redirect. Within ProcessP you can access the data utilizing the key you previously used:

var whatever = TempData["Key"];

For a decent read, I would recommend reading through this thread: ASP.NET MVC - TempData - Good or bad practice

Community
  • 1
  • 1
Jesse
  • 8,223
  • 6
  • 49
  • 81
  • ok; I will read it. So, can TempData hold a model? or is it like ViewData where it can only hold value pair? Thanks! – scv Jun 09 '12 at 00:51
  • yes tempdata can hold your model, or whatever object you feel like placing within. – Jesse Jun 09 '12 at 00:53
  • 1
    Just remember you have to cast it to the type inside TempData. Such as var model = (MyViewModel)TempData["Key"]; – CD Smith Jun 09 '12 at 02:01