Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

on html.actionlink click go to previous page

Currently in a link

Customer/businessunit/RepresentativeDetails?RepresentativeId=cd3a7263-78f7-41bd-9eb0-12b30bc1059a

I have following code for view

@Html.ActionLink("Back to List", "Index") 

which takes me to this link

customer/businessunit/index 

but rather that going to index page I want to go to previous page when the actionlink is clicked, which is

Customer/businessunit/BusinessUnitDetails/c4a86253-a287-441e-b83d-71fbb6a588bc 

How do I create an actionlink that directs me to previous page? something like @Html.ActionLink("Back to Details", //go to previous page)

like image 549
Cybercop Avatar asked Aug 27 '13 12:08

Cybercop


People also ask

What is HTML ActionLink ()?

ActionLink(HtmlHelper, String, String) Returns an anchor element (a element) for the specified link text and action. ActionLink(HtmlHelper, String, String, Object) Returns an anchor element (a element) for the specified link text, action, and route values.

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

How do I pass an object in ActionLink?

If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).

How do I post on ActionLink?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to submit (post) Form in ASP.Net MVC 5 Razor. Hence in order to submit (post) Form using @Html. ActionLink, a jQuery Click event handler is assigned and when the @Html.


1 Answers

Unless you're tracking what the previous page is on the server, why not just use the browser's internal history? In that case there wouldn't be a need for server-side code. You could just use something like this:

<a href="javascript:void(0);" onclick="history.go(-1);">Back to Details</a> 

Or, separating the code from the markup:

<a href="javascript:void(0);" id="backLink">Back to Details</a>  <script type="text/javascript">     $(document).on('click', '#backLink', function () {         history.go(-1);     }); </script> 

This would send the user back to whatever was the last page in their browser history. (Of course, if they reached that page from any other source then it wouldn't take them "back to details" but instead just "back".)

like image 192
David Avatar answered Oct 14 '22 03:10

David