1

I have a master detail scenario going from a list of categories to ingredients.

In the Edit ActionResult I have:

if (ModelState.IsValid){
    dc.Entry(mainingredient).State = EntityState.Modified;
    dc.SaveChanges();
    int ID = ?????
    return RedirectToAction("Details", new { id = ID});
}
...

I am basically trying to go back to the page I came from. For example... from /Ingredient/Edit/2 back to Ingredient/Details/2

To make this question clearer: how do I pass an id from the edit get to edit httppost controller so that I can redirect the user back to the details page again passing the id after they make an update?

2
  • 1
    What is the question? return RedirectToAction("Details", new { id = ID}); is in MVC style :)
    – Samich
    Sep 12, 2011 at 6:42
  • @Samich I forgot to mention this is on the HttpPost of controller. I simply do not know how to get the id from the Edit "get" controller to the "post".
    – Par6
    Sep 12, 2011 at 12:50

1 Answer 1

5

You could pass a url query string parameter to the Edit action when invoking it. This way you will be able to redirect to:

public ActionResult Edit(string returnUrl)
{
    ...
    return Redirect(url);
}

or if you already know the controller and action:

return RedirectToAction("Details", new { id = ID });

will be sufficient. The ID you want to redirect back could be passed as action argument.

2
  • thank you. I have edited my question because what I do not know is how to go from the Edit httppost back to the details page. Some viewbag to grab the id?
    – Par6
    Sep 12, 2011 at 23:07
  • @Par6, if you have the id in the GET action you could simply set it as a property of the view model that's being sent to the view and inside this view use a hidden field t render the value. Then when the form is submitted to the POST action the id will be posted back and you will be able to retrieve it. Sep 12, 2011 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.