I have sample code like this:
<div class="cart">
<a onclick="addToCart('@Model.productId');" class="button"><span>Add to Cart</span></a>
</div>
<div class="wishlist">
<a onclick="addToWishList('@Model.productId');">Add to Wish List</a>
</div>
<div class="compare">
<a onclick="addToCompare('@Model.productId');">Add to Compare</a>
</div>
How can I write JavaScript code to call the controller action method?
You can simply add this when you are using same controller to redirect
If you do not need much customization and seek for simpleness, you can do it with built-in way - AjaxExtensions.ActionLink method.
That MSDN link is must-read for all the possible overloads of this method and parameters of AjaxOptions class. Actually, you can use confirmation, change http method, set OnSuccess and OnFailure clients scripts and so on