Mvc ActionLink with javascript

I am working with MVC, I have a view with ActionLink that calls my controller action, my problem is when I want to call the javascript function in the onClick () event of this action link (how this action link is converted to the standard html tag during fulfillment). How can I do it? what's better? Here is the code for my ActionLink:

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

Thanks.

+3
source share
1 answer

Give the link an identifier (or class) and unobtrusively apply a handler using javascript. An example of 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>
+12
source

Source: https://habr.com/ru/post/1725413/


All Articles