单击按钮时如何重定向用户?

发布于 2024-09-08 16:04:37 字数 158 浏览 1 评论 0原文

我有一个带有按钮的视图。当用户单击按钮时,我希望他们重定向到数据输入视图。我该如何实现这个目标?我应该提到的是,视图已经创建、测试并运行。我可以通过输入网址来找到它们。

我寻找了解释如何连接按钮的 onclick 事件的步骤,但我是 MVC 新手,此时有点迷失。

谢谢!

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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

¢好甜 2024-09-15 16:04:37

这取决于您所说的按钮的含义。如果它是一个链接:

<%= Html.ActionLink("some text", "actionName", "controllerName") %>

为了发布,您可以使用一个表单:

<% using(Html.BeginForm("actionName", "controllerName")) { %>
    <input type="submit" value="Some text" />
<% } %>

最后,如果您有一个按钮:

<input type="button" value="Some text" onclick="window.location.href='<%= Url.Action("actionName", "controllerName") %>';" />

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") %>';" />
小霸王臭丫头 2024-09-15 16:04:37

作为对其他答案的补充,这里是剃刀引擎语法:

<input type="button" value="Some text" onclick="@("window.location.href='" + @Url.Action("actionName", "controllerName") + "'");" />

window.location.href = '@Url.Action("actionName", "controllerName")';

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")';
幽梦紫曦~ 2024-09-15 16:04:37

如果您像我一样不喜欢依赖 JavaScript 来获取按钮上的链接。您还可以使用锚点并使用 CSS 设计其样式,就像按钮一样。

<a href="/Controller/View" class="Button">Text</a>

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.

<a href="/Controller/View" class="Button">Text</a>
唯憾梦倾城 2024-09-15 16:04:37

如果使用 JQuery,你可以这样做:

<script type="text/javascript">
    $('#buttonid').click(function () {
        document.location = '@Url.Action("ActionName","ControllerName")';
    });
</script>

if using JQuery, you can do this :

<script type="text/javascript">
    $('#buttonid').click(function () {
        document.location = '@Url.Action("ActionName","ControllerName")';
    });
</script>
看透却不说透 2024-09-15 16:04:37

或者,如果上述方法都不起作用,那么您可以使用以下方法,因为它对我有用。

想象一下这是你的按钮,

<button class="btn" onclick="NavigateToPdf(${Id});"></button>

我使用 jquery 模板填充了 ${Id} 的值。您可以使用任何适合您要求的东西。在下面的函数中,我将 window.location.href 设置为等于控制器名称,然后是操作名称,最后是参数。我能够成功导航。

function NavigateToPdf(id) {
    window.location.href = "Link/Pdf/" + id;
}

我希望它有帮助。

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.

function NavigateToPdf(id) {
    window.location.href = "Link/Pdf/" + id;
}

I hope it helps.

茶花眉 2024-09-15 16:04:37

根据我的经验,ASP MVC 确实不太喜欢传统的按钮使用方式。相反,我使用:

  <input type="button" class="addYourCSSClassHere" value="WordsOnButton" onclick="window.location= '@Url.Action( "ActionInControllerHere", "ControllerNameHere")'" />

It has been my experience that ASP MVC really does not like traditional use of button so much. Instead I use:

  <input type="button" class="addYourCSSClassHere" value="WordsOnButton" onclick="window.location= '@Url.Action( "ActionInControllerHere", "ControllerNameHere")'" />
我的鱼塘能养鲲 2024-09-15 16:04:37

您可以轻松地用标签包裹按钮标签。使用 Url.Action() HTML Helper 这将导航到另一个页面。

<a href='@Url.Action("YourAction", "YourController")'>
    <input type='button' value='Dummy Button' />
</a>

如果您想使用 javascript onclick 进行导航() 函数然后使用

<input type='button' value='Dummy Button' onclick='window.location = "@Url.Action("YourAction", "YourController")";' />

You can easily wrap your button tag with tag.Using Url.Action() HTML Helper this will get to navigate to one page to another.

<a href='@Url.Action("YourAction", "YourController")'>
    <input type='button' value='Dummy Button' />
</a>

If you want to navigate with javascript onclick() function then use

<input type='button' value='Dummy Button' onclick='window.location = "@Url.Action("YourAction", "YourController")";' />
痴骨ら 2024-09-15 16:04:37

正确答案:上面的答案是正确的(对于其中一些答案),但让我们简单一点——用一个标签。

我更喜欢使用输入标签,但您可以使用按钮标签

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

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:

< 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
你的他你的她 2024-09-15 16:04:37
$("#yourbuttonid").click(function(){ document.location = "<%= Url.Action("Youraction") %>";})
$("#yourbuttonid").click(function(){ document.location = "<%= Url.Action("Youraction") %>";})
裂开嘴轻声笑有多痛 2024-09-15 16:04:37
<li class="nav-item">
    <a class="nav-link text-dark" asp-area="" asp-page="/UsersPage">Users</a>
</li>

试试这个

<li class="nav-item">
    <a class="nav-link text-dark" asp-area="" asp-page="/UsersPage">Users</a>
</li>

Try this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文