CoolArchTek
CoolArchTek

Reputation: 3849

Assign ActionLink to jQuery button in MVC

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

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

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

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

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

Upvotes: 7

Views: 18185

Answers (4)

Mathew Thompson
Mathew Thompson

Reputation: 56459

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:

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

Upvotes: 8

xivo
xivo

Reputation: 2054

<%: 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.

Upvotes: 0

Felipe Oriani
Felipe Oriani

Reputation: 38638

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

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

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

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

Upvotes: 0

gdoron
gdoron

Reputation: 150313

Give the anchor an 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 // <==============
)

Upvotes: 2

Related Questions