0

I have an MVC 4 app that contains a View where the user can enter search criteria (in a form) and then displays results in a table below the search form. For each row in the table, the user can click on a link that loads another View that shows him/her details about the item selected.

On this details view, I have a link that takes the user back to the from he/she can from using the following: @Html.ActionLink("Back to List", "Index"). When the user clicks this link and returns to the search form, all the search criteria he/she entered is gone. However, if the user clicks the Back button on the browser, the search criteria is maintained. Is there a way I can call ActionLink that mimics what the Back button is doing? Or maybe JavaScript? I am very new to MVC and JavaScript.

tereško
  • 58,060
  • 25
  • 98
  • 150
Hosea146
  • 7,412
  • 21
  • 60
  • 81

2 Answers2

4

The easiest way is to use javascript history API:

window.history.back();

here is the fiddle

user20140268
  • 1,313
  • 11
  • 16
0

Here a HtmlHelper extension that does the trick.

public static class Helpers
{
    public static MvcHtmlString BackViaHistory(this HtmlHelper helper, string linkText)
    {
        return new MvcHtmlString("<a onclick='javascript: window.history.back();'>" + linkText + "</a>");
    }
}
Vincent
  • 155
  • 3
  • 11