javascript 将 ActionLink 分配给 MVC 中的 jQuery 按钮

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

Assign ActionLink to jQuery button in MVC

c#javascriptjqueryasp.net-mvcasp.net-mvc-3

提问by CoolArchTek

How do I assign ActionLink to a jQuery button in asp.net MVC page.

如何将 ActionLink 分配给 asp.net MVC 页面中的 jQuery 按钮。

<button id="Button1">Click</button>

<%: Html.ActionLink("About", "About", "Home")%>

<script language="javascript" type="text/javascript">
    $(function () {

        $("#Button1").button().click(function () {
            return false;
        });
    });
</script>

回答by mattytommo

Based on your comments, you just want to go to the About action in the Home controller on button click, you can do the following and remove the ActionLink:

根据您的评论,您只想在单击按钮时转到 Home 控制器中的 About 操作,您可以执行以下操作并删除ActionLink

$(function () {
    $("#Button1").click(function () {
        location.href = '<%= Url.Action("About", "Home") %>';
    });
});

回答by gdoron is supporting Monica

Give the anchor an id:

给锚一个id

<%: Html.ActionLink("About", "About", "Home", null, new { id = "Button1"})%>

Using this overload:

使用这个重载

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes // <==============
)

回答by Felipe Oriani

The buttonmethod of jQuery works for a tag too, try this:

jQuery的按钮方法也适用于标签,试试这个:

<%: Html.ActionLink("About", "About", "Home", null, new { id="Button1" })%>

<script language="javascript" type="text/javascript">
    $(function () {

        $("#Button1").button().click(function () {
            return false;
        });
    });
</script>

回答by xivo

<%: Html.ActionLink("About", "About", "Home", new { @class = "yourclass" })%>

You can try attaching a class to the actionlink and you can select it with query. If you want to use a class.

您可以尝试将一个类附加到操作链接,然后您可以通过查询选择它。如果你想使用一个类。