Click <%: ...">

Assign ActionLink to jQuery Button in MVC

How to assign ActionLink to jQuery button on 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> 
+7
source share
4 answers

According to your comments, you just want to go to the "About" window in the main controller when you click a button, you can do the following and remove ActionLink :

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

Give the id binding:

 <%: 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 // <============== ) 
+2
source

button jQuery method also works with a tag, try the following:

 <%: Html.ActionLink("About", "About", "Home", null, new { id="Button1" })%> <script language="javascript" type="text/javascript"> $(function () { $("#Button1").button().click(function () { return false; }); }); </script> 
0
source
 <%: Html.ActionLink("About", "About", "Home", new { @class = "yourclass" })%> 

You can try connecting the class to actionlink, and you can select it with the request. If you want to use a class.

0
source

All Articles