Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

5 Answers

up vote 9 down vote accepted

Use jQuery ajax:

  function AddToCart(id)
  {
    $.ajax({
      url: 'urlToController',
      data: { id: id }
      success: function(){
        alert('Added');      
      }
    });
  }

http://api.jquery.com/jQuery.ajax/

share|improve this answer
1  
thanks Kman, its working ... – Milian Jan 21 '12 at 12:36
There is a syntax error with the example above, as there should be a comma after data: { id: id }, but unfortunately it is deemed too trivial an edit for me to fix. – Sterno Apr 9 at 17:38

If you want to call an action from your JavaScript, one way is to embed your JavaScript code, inside your view (.cshtml file for example), and then, use Razor, to create a URL of that action:

$(function(){
    $('#sampleDiv').click(function(){
        /*
         While this code is JavaScript, but because it's embedded inside
         a cshtml file, we can use Razor, and create the URL of the action
         */
        var url = @Url.Action("ActionName", "Controller");
    });
});
share|improve this answer
thanks saeed, for your useful help ... – Milian Jan 21 '12 at 12:35

jQuery post is the short version of jQuery ajax.

function addToCart(id)
{
  $.post('cartController',{id:id } function(data) {
    //do whatever with the result.
  });
}

If you want more options like success callbacks and error handling, use jQuery ajax,

function addToCart(id)
{
  $.ajax({
  url: "cartController",
  data: { id: id },
  success: function(data){
    //call is successfully completed and we got result in data
  },
  error:function (xhr, ajaxOptions, thrownError){
                  //some errror, some show err msg to user and log the error  
                  alert(xhr.responseText);

                }    
  });
}
share|improve this answer

If you do not need much customization and seek for simpleness, you can do it with built-in way - AjaxExtensions.ActionLink method.

<div class="cart">
      @Ajax.ActionLink("Add To Cart", "AddToCart", new { productId = Model.productId }, new AjaxOptions() { HttpMethod = "Post" });
</div>

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

share|improve this answer

You can set up your element with value="@model.productId" and onclick= addToWishList(this.value);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.