Can you pass a model with RedirectToAction?

Jeremy picture Jeremy · Dec 24, 2009 · Viewed 7.7k times · Source

I'm using mvc 2 release candidate, and am wondering if there's any way to pass a model to an action using RedirectToAction.

For example, I have an edit action which takes an ID, and loads the record from a database, displays the current values in text boxes and lets the user edit and click submit:

public ActionResult Edit(int ID)

Then I have an edit action for the HttpPost which takes a model and updates the database:

[HttpPost]
public ActionResult Edit(Administration.Models.ManagementCompanyModel model)

Because I already have the model containing the new data, I don't want to simply re-direct to the Details action, I want to somehow redirect to the details action and pass the model. Possible?

Answer

Alexander Taran picture Alexander Taran · Dec 24, 2009
TempData["Model"] = YourModel;
Return RedirectToAction("details");

and in details action, check for TempData["Model"] != null and grab it from there