user1907849
user1907849

Reputation: 980

Determining the previous page in mvc 3

How to check the previous page in mvc 3 application.

<a href="#">Previous Page.</a>

On click of the above link I have to go back to previous page.

How to do this ?

Upvotes: 0

Views: 1288

Answers (2)

Jim Neff
Jim Neff

Reputation: 326

Same as Matthew's answer but using a session variable. That way you could update it selectively in the Action you want. For example, on a POST action you wouldn't want them to go back to that view with form values there. What you really want is for them to go back to the page before that.

public ActionResult MyNextPage(string prevUrl)
{
    Session["prevUrl "] = prevUrl;
    View();
}

Then in the View:

<a href="@Session["assigned_section_id"].ToString()">Previous Page</a>

Note that if session is expired or null it will throw an exception.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

This will depend on how is the navigation organized on your website. One possibility is to use the history.go(-1) javascript function which will simply simulate the browser back button:

<a href="#" onclick="history.go(-1)">Previous Page.</a>

Another possibility is to have the calling page pass a ReturnUrl query string parameter to this page which could be used to construct the link:

<a href="@Request["ReturnUrl"]">Previous Page.</a>

Of course this assumes that when you called the controller action that rendered this view you have passed the ReturnUrl query string parameter.

Upvotes: 2

Related Questions