Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mvc ActionLink with javascript

Tags:

asp.net-mvc

I'm working with MVC, I have a view with an ActionLink that calls an Action of my controller, my issue is when I want to call also a javascript function on the onClick() event of that action link (as that action link converts to html standart tag on execution time). How should i do this? what is the better way? Here is the code of my ActionLink:

<%=Html.ActionLink("View Report", "GeneratePdf", new { strProductId = myObject.productId})%>

Thanks.

like image 709
lidermin Avatar asked Mar 21 '26 06:03

lidermin


1 Answers

Give the link an id (or class) and apply the handler unobtrusively using javascript. Example using jQuery:

<%=Html.ActionLink("View Report", "GeneratePdf",
     new { strProductId = myObject.productId},
     new { id = "reportLink" } )%>


<script type="text/javascript">
    $(function() {
        $('#reportLink').click( function() {
             ... do what you need to do...
             // return false; // to cancel the default action of the link
        });
    });
</script>
like image 102
tvanfosson Avatar answered Mar 22 '26 20:03

tvanfosson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!