Jeremy
Jeremy

Reputation: 46340

Can you pass a model with RedirectToAction?

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?

Upvotes: 15

Views: 7729

Answers (1)

Alexander Taran
Alexander Taran

Reputation: 6725

TempData["Model"] = YourModel;
Return RedirectToAction("details");

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

Upvotes: 17

Related Questions