Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 892
CoolArchTek Avatar asked May 15 '12 17:05

CoolArchTek


People also ask

How to give ActionLink in MVC?

Use an underscore instead of a dash and MVC will automatically replace the underscore with a dash in the rendered HTML, here it is: @Html. ActionLink("Edit", // <-- Link text. "Edit", // <-- Action Method Name.

How can we call post method using ActionLink in MVC?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to submit (post) Form in ASP.Net MVC 5 Razor. Hence in order to submit (post) Form using @Html. ActionLink, a jQuery Click event handler is assigned and when the @Html.

What is HTML ActionLink ()?

ActionLink(HtmlHelper, String, String, String, String, String, String, Object, Object) Returns an anchor element (a element) for the specified link text, action, controller, protocol, host name, URL fragment, route values, and HTML attributes.


1 Answers

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") %>';
    });
});
like image 167
mattytommo Avatar answered Sep 21 '22 21:09

mattytommo