on html.actionlink click go to previous page
Asked Answered
H

7

36

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)

Hohenstaufen answered 27/8, 2013 at 12:48 Comment(1)
Possible duplicate of ActionLink back buttonAmadaamadas
H
79

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".)

Hierarchize answered 27/8, 2013 at 12:55 Comment(1)
I have very rare case , my app consist with two parts blog and main part, blog is written in mvc , main app is written in agular+api, when i go from my angular app to the mvc app and then press back button from browser, page does not refresshed. Can you pleaese give me some idea how can i solve this?Regolith
A
48

If you still want to use ActionLink you can do something like as suggested by JuanPieterse

@Html.ActionLink("Back to previous page", null, null, null, new { href = Request.UrlReferrer})

You can use action in controller too. See answers to similar question here

Acidulate answered 16/2, 2015 at 23:34 Comment(0)
D
13

Don't use ActionLink for this... just do:

<a href="javascript:history.back()">Back to List</a>

...which will take the user back to wherever they were prior to the current page

Demimondaine answered 27/8, 2013 at 12:55 Comment(1)
I have very rare case , my app consist with two parts blog and main part, blog is written in mvc , main app is written in agular+api, when i go from my angular app to the mvc app and then press back button from browser, page does not refresshed. Can you pleaese give me some idea how can i solve this?Regolith
C
12

If you don't like to use ActionLink or JavaScript, the href="@Request.UrlReferrer" will do the trick:

<div>
    <a href="@Request.UrlReferrer" class="btn btn-default btn-lg" title="Back to list">
        <span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>
    </a>
</div>
Conjunction answered 14/5, 2016 at 12:37 Comment(3)
Brilliant. The best answer here IMO. Simple. No inline JS or odd looking ActionLink.Lefthanded
On refresh page @Request.UrlReferre = nullLiverpool
I have very rare case , my app consist with two parts blog and main part, blog is written in mvc , main app is written in agular+api, when i go from my angular app to the mvc app and then press back button from browser, page does not refresshed. Can you pleaese give me some idea how can i solve this?Regolith
N
2

This is quite a bit after the fact, but I thought I'd contribute. Personally, I would tag my markup elements with a CSS class so I could just reuse the tag and be done with it.

Markup:

<a href="" class="go_back"> Back </a>

Script:

<script type="text/javascript">
    $(function () {
        $('.go_back').click(function (e) {
            e.preventDefault();
            history.go(-1);
        });
    });
</script>
Nofretete answered 6/2, 2014 at 22:8 Comment(0)
A
0

Use ActionLink:

@Html.ActionLink("بازگشت", null, null, null, new { href = Request.UrlReferrer })
Alforja answered 8/4, 2020 at 7:48 Comment(1)
How is this answer different from the one provided by "aim" on Feb 16' 15?Hohenstaufen
L
0

i know this is too late but i got this answer.

<button type='reset' class='btn btn-danger' onclick='window.location.go(-1);'><i class='fa fa-ban'></i> Cancel </button></div>
Landside answered 25/8, 2021 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.