Simple MVC Redirect after Action Completion
I ran across a rather simple problem today that I did not find a lot of simple documentation to explain so here is my simple example to hopefully help whoever might stumble upon this same question.
The Scenario:
I have a simple controller that posts data to an API, at the conclusion it hands back the newly inserted item so I can display it to my user. However the action to add the item is not the same action that displays the newly inserted item. The problem is how do I hand off the newly inserted item to the other action handler.
The Solution:
Pretty simple in the long run using the “RedirectToAction” method. In the end it looks a lot like this:
return RedirectToAction("Get", new RouteValueDictionary() { { "id", response.Id } });
I simply tell the method where to go and create a simple value dictionary object with my single Guid to pass on and lookup the object. All in all pretty simple and you can even build up the dictionary to add other objects if you needed to.
Brent