ASP.NET MVC2 - Return to previous View from a controller action
How can I return to the "previous" view from an action in 开发者_JAVA百科the controller ?
I have an action that can be reached from 2 different views.
The action can result in 2 ways:
Success - and then I return to 3rd different view Failure - and then I want to return to the previous view (one of the 2) - having the same (old) model data with a TempData["Message"] containing the error.
I want to return to the view that posted to this action - How can I achieve this behavior ?
You can pass previous view name as a parameter to your action.
public ActionResult Edit(DataObject model, string prevAction)
{
if (model.IsValid)
{
return View("SomeView");
}
else
{
return RedirectToAction(prevAction, new { data = model });
}
}
You could have a query string variable that holds the location to where you would like to return.
Something like controller/action?returnUrl=original/route
精彩评论