MVC Views, Returning to the same view after edit

I’d like to share how to return the same view after adding or editing content to the current item.

I had the case where I was editing a transportation record where I can have one or more containers (in my example).

Since I’m modifying an specific transportation, in the get version of the ActionResult method, is a parameter, lets say:

 

public ActionResult Create(Guid id)
{
...
}

Once I make the addition, at the POST version of the ActionResult method, I want the user to stay at the same view, for current Transport. if I used something like:

...
return View("Create", TransportationId)
...

I got the user back to the same view (page) but he/she can still see the same values just entered before saving, which is confusing and made users to sometimes add containers records more than once. In order to solve that situation I used:

...
return RedirectToAction("Create", new {id=TransportationId})
...

RedirectToAction returns a response to the browser and causes it to make a GET request to Create Action. The new… part carries the route parameter (as parameters are included on route querystring.

This way, when users adds the container, he/she can see the same page but with empty values so can keep entering more containers without thinking previous save was not successful.

Cheers.