Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 943
Jeremy Avatar asked Dec 24 '09 22:12

Jeremy


People also ask

Can we pass model in RedirectToAction?

Finally, the PersonModel class object is passed to the RedirectToAction method along with the name of the destination Controller and its Action method. The Controller consists of the following Action method. Inside this Action method, the PersonModel class object is received.

What is the usage of a RedirectToAction?

The RedirectToAction() Method This method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action. This acts just like as Response.

What is difference between RedirectToAction and RedirectToRoute?

RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .


1 Answers

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

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

like image 149
Alexander Taran Avatar answered Nov 15 '22 23:11

Alexander Taran