Calling ASP.NET MVC Action Methods from JavaScript

后端 未结 8 1345
谎友^
谎友^ 2020-11-29 23:45

I have sample code like this:

 
      
8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 00:04

    Javascript Function
    
    function AddToCart(id) {
     $.ajax({
       url: '@Url.Action("AddToCart", "ControllerName")',
       type: 'GET',
       dataType: 'json',
       cache: false,
       data: { 'id': id },
       success: function (results) {
            alert(results)
       },
       error: function () {
        alert('Error occured');
       }
       });
       }
    
    Controller Method to call
    
    [HttpGet]
      public JsonResult AddToCart(string id)
      {
        string newId = id;
         return Json(newId, JsonRequestBehavior.AllowGet);
      }
    

提交回复
热议问题