Html 使用锚标记来调用操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18713862/
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-08-29 13:13:18  来源:igfitidea点击:

Use anchor tag for calling an Action

htmlasp.net-mvc-3

提问by Syspect

First I tried with

首先我试过

<input type="button" value="Something" onclick="location.href='@Url.Action("ActionName")'" />

But this is a button and I want just a link. I've seen somewhere (while wildly browsing the programming forums) a way to hide a button link behind an anchor, but couldn't find/make it.

但这是一个按钮,我只想要一个链接。我在某处(在疯狂浏览编程论坛时)看到过一种将按钮链接隐藏在锚点后面的方法,但无法找到/制作它。

Then I tried with

然后我试过

<a href="@Html.ActionLink("Something", "ActionName", "ControllerName")"></a>

(here, actually, on the page, the link looked like Something">, whereas the " and > shouldn't be there)

(在这里,实际上,在页面上,链接看起来像Something">,而" 和> 不应该在那里)

and also

并且

<a href="@Url.Action("ActionName", "ControllerName")">Something</a>

but the latter 2 ways (with the anchor tag) don't work, by means, I don't get directed to the other page. So, any suggestions about how to hide a button behind a link, or how to make the anchor tags run ?

但是后两种方式(使用锚标记)不起作用,也就是说,我不会被定向到另一个页面。那么,关于如何隐藏链接后面的按钮或如何使锚标记运行的任何建议?

回答by Milkncookiez

You want an anchor, but the suggested code doesn't work. Try this way (it worked for me):

您想要一个锚点,但建议的代码不起作用。试试这种方式(它对我有用):

<a onclick="location.href='@Url.Action("ActionName")'">Something</a><br />

It will work this way, but you will not have the hand, when you hover the link. So just add href="#", like that:

它将以这种方式工作,但是当您悬停链接时,您将没有手。所以只需添加href="#",就像这样:

<a href="#" onclick="location.href='@Url.Action("ActionName")'">Something</a><br />

回答by Boranas

Use @Html.ActionLink("Something", "ActionName", "ControllerName")without the <a>tag. It's generated automatically

@Html.ActionLink("Something", "ActionName", "ControllerName")不带<a>标签使用。它是自动生成的