Reputation: 9027
Let's say I have a POST action in controller that saves a record to a database and returns back to view. I do something like:
[HTTP POST]
public ActionView Save()
{
//....do stuff
return View(); //This will return back to /ControllerName/Index
//or I can do something like this:
return View("ViewName") //this will return to /ControllerName/ViewName
}
But, let's say I call Save method from multiple views. What is the best way to return back to a view that called an action? Should I store View name in query string? or in a view bag? Maybe there's a better approach?
Upvotes: 0
Views: 101
Reputation: 15931
I would use different actions and redirect to the calling url. The pattern is called post redirect get and is used a lot.
This way you get clear urls and refreshing the browser still works. You might think that gets you some duplicated code. Keep the code in the ViewModel and Service and everything should be ok.
Soemthing like this:
[HTTP POST]
public ActionView SaveCustomer(string viewName)
{
return RedirectToAction("DisplayCustomer")
}
[HTTP POST]
public ActionView SaveProject(string viewName)
{
return RedirectToAction("DisplayProject")
}
Upvotes: 0
Reputation: 39278
If I'm understanding you correctly you want to share this action and invoke it from many different sources?
Since this is a post action you can send the name of the view in the post collection as part of the post request.
[HTTP POST]
public ActionView Save(string viewName)
{
return View(viewName);
}
Upvotes: 2