asp.net-mvc 单击按钮时如何重定向用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3168577/
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
How do I redirect a user when a button is clicked?
提问by DenaliHardtail
I have a view with a button. When the user clicks the button I want them redirected to a data entry view. How do I accomplish this? I should mention the views are created, tested, and functioning. I can get to them by typing the url.
我有一个带按钮的视图。当用户单击按钮时,我希望他们重定向到数据输入视图。我该如何实现?我应该提到视图已创建、测试和运行。我可以通过输入网址找到他们。
I looked for steps explaining how to wire up the onclick event of the button but I'm new to MVC and kinda lost at this point.
我寻找了解释如何连接按钮的 onclick 事件的步骤,但我是 MVC 的新手,在这一点上有点迷失。
Thanks!
谢谢!
回答by Darin Dimitrov
It depends on what you mean by button. If it is a link:
这取决于您所说的按钮是什么意思。如果是链接:
<%= Html.ActionLink("some text", "actionName", "controllerName") %>
For posting you could use a form:
对于发布,您可以使用表格:
<% using(Html.BeginForm("actionName", "controllerName")) { %>
<input type="submit" value="Some text" />
<% } %>
And finally if you have a button:
最后,如果你有一个按钮:
<input type="button" value="Some text" onclick="window.location.href='<%= Url.Action("actionName", "controllerName") %>';" />
回答by AGuyCalledGerald
Just as an addition to the other answers, here is the razor engine syntax:
就像其他答案的补充一样,这里是剃刀引擎语法:
<input type="button" value="Some text" onclick="@("window.location.href='" + @Url.Action("actionName", "controllerName") + "'");" />
or
或者
window.location.href = '@Url.Action("actionName", "controllerName")';
回答by Cherubic
if using JQuery, you can do this :
如果使用 JQuery,你可以这样做:
<script type="text/javascript">
$('#buttonid').click(function () {
document.location = '@Url.Action("ActionName","ControllerName")';
});
</script>
回答by Dustin Laine
If, like me, you don't like to rely on JavaScript for links on buttons. You can also use a anchor and style it like your buttons using CSS.
如果像我一样,您不喜欢依赖 JavaScript 来获取按钮上的链接。您还可以使用锚点并使用 CSS 对其进行样式设置,就像您的按钮一样。
<a href="/Controller/View" class="Button">Text</a>
回答by Mandeep Janjua
Or, if none of the above works then you can use following approach as it worked for me.
或者,如果上述方法均无效,那么您可以使用以下方法,因为它对我有用。
Imagine this is your button
想象一下这是你的按钮
<button class="btn" onclick="NavigateToPdf(${Id});"></button>
I got the value for ${Id} filled using jquery templates. You can use whatever suits your requirement. In the following function, I am setting window.location.href equal to controller name then action name and then finally parameter. I am able to successfully navigate.
我使用 jquery 模板填充了 ${Id} 的值。您可以使用任何适合您的要求。在下面的函数中,我将 window.location.href 设置为等于控制器名称,然后是动作名称,最后是参数。我能够成功导航。
function NavigateToPdf(id) {
window.location.href = "Link/Pdf/" + id;
}
I hope it helps.
我希望它有帮助。
回答by Brijesh Mavani
You can easily wrap your button tag with tag.Using Url.Action() HTML Helperthis will get to navigate to one page to another.
你可以很容易地用 tag.Using Url.Action() HTML Helper包装你的按钮标签,这将导航到一个页面到另一个页面。
<a href='@Url.Action("YourAction", "YourController")'>
<input type='button' value='Dummy Button' />
</a>
If you want to navigate with javascript onclick()function then use
如果您想使用 javascript onclick()函数导航,请使用
<input type='button' value='Dummy Button' onclick='window.location = "@Url.Action("YourAction", "YourController")";' />
回答by Guest
RIGHT ANSWER HERE: Answers above are correct (for some of them) but let's make this simple -- with one tag.
正确答案:上面的答案是正确的(对于其中一些),但让我们简化一下——用一个标签。
I prefer to use input tags, but you can use a button tag
我更喜欢使用输入标签,但你可以使用按钮标签
Here's what your solution should look like using HTML:
以下是您的解决方案使用 HTML 时的样子:
< input type="button" class="btn btn-info" onclick='window.location.href = "@Url.Action("Index", "ReviewPendingApprovals", routeValues: null)"'/> // can omit or modify routeValues to your liking
回答by Henry.D
It has been my experience that ASP MVC really does not like traditional use of button so much. Instead I use:
根据我的经验,ASP MVC 真的不太喜欢按钮的传统使用。相反,我使用:
<input type="button" class="addYourCSSClassHere" value="WordsOnButton" onclick="window.location= '@Url.Action( "ActionInControllerHere", "ControllerNameHere")'" />
回答by PsyhoLord
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/UsersPage">Users</a>
</li>
Try this
尝试这个
回答by Gregtheitroade
$("#yourbuttonid").click(function(){ document.location = "<%= Url.Action("Youraction") %>";})

