asp.net-mvc 在 html.actionlink 点击转到上一页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18465895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:07:37  来源:igfitidea点击:

on html.actionlink click go to previous page

asp.net-mvcasp.net-mvc-4routinghtml.actionlink

提问by Cybercop

Currently in a link

当前在链接中

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

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)

如何创建将我定向到上一页的操作链接?就像是@Html.ActionLink("Back to Details", //go to previous page)

回答by David

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

这会将用户发送回浏览器历史记录中的最后一页。(当然,如果他们从任何其他来源到达该页面,那么他们不会“回到细节”,而只是“返回”。)

回答by aim

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

如果您仍然想使用,ActionLink您可以按照JuanPieterse 的建议进行操作

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

You can use actionin controllertoo. See answers to similar question here

您可以使用actioncontroller了。在此处查看类似问题的答案

回答by Robert Levy

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

不要为此使用 ActionLink ......只需执行以下操作:

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

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

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

...这会将用户带回当前页面之前的任何位置

回答by Stacked

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

如果您不喜欢使用ActionLink或 JavaScript,则href="@Request.UrlReferrer"可以解决这个问题:

<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>

回答by farzad

Use ActionLink:

使用操作链接:

@Html.ActionLink("??????", null, null, null, new { href = Request.UrlReferrer })

回答by Dylan Hayes

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.

这是事后的相当多的事,但我想我会有所贡献。就我个人而言,我会用 CSS 类标记我的标记元素,这样我就可以重复使用该标记并完成它。

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>