Reputation: 5301
I have a page (Controller Action that renders a View) that a user could navigate to from 3 different pages. Basically, a user gets there, does a few selections and clicks on Save button. At this point, I need to redirect the user to where he came from.
I'm wondering, what's the best practice to do that?
I know, for example, I could look in Request, figure out where he came from, then redirect back to there... But that doesn't look like the ideal approach to me.
Upvotes: 2
Views: 5748
Reputation: 20430
(Posted on behalf of the question author).
This is what I ended up doing.
Controller:
public ActionResult Index()
{
ViewBag.Referrer = Request.UrlReferrer.LocalPath;
//.....
return View();
}
View (Razor syntax):
<a href="@ViewBag.Referrer">Back</a>
Upvotes: 0
Reputation: 1038720
I'm wondering, what's the best practice to do that?
Pass a returnUrl
parameter when invoking this action. Store it in a hidden field if necessary. Make sure that the controller action that performs the validation and needs to redirect gets this parameter as action argument somehow and when the time comes return Redirect(returnUrl);
Upvotes: 7