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>
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.
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.
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.
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") %>';
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With