2014-09-20 56 views
0

試圖傳遞一個id回控制器(順便說一句,id不爲空) 控制器認爲它得到空。jquery ajax發送數據 - ASP.Net MVC

var url = '@Url.Action("Delete")'; 

     $.ajax({ 
      url: url, 
      async: false, 
      data: {id:clientId},// have also tried data:clientId and data:[clientId] 
      type: 'POST', 
     }); 

控制器

public ActionResult Delete(string id) 
     { 
... 
} 

回答

1

這裏去實施 -

public ActionResult Delete(string id) 
    { 
     return null; 
    } 

說你jQuery代碼和HTML是如下 -

<script src="~/Scripts/jquery-1.8.2.min.js"></script> 
<script> 
    function submitForm() { 
     var model = new Object(); 
     model.Name = "Rami"; 

     jQuery.ajax({ 
      type: "POST", 
      url: "@Url.Action("Delete")", 
      dataType: "json", 
      contentType: "application/json; charset=utf-8", 
      data: JSON.stringify({ id: model.Name }), 
      success: function (data) { alert(data); }, 
      failure: function (errMsg) { 
       alert(errMsg); 
      } 
     }); 
    } 
</script> 

<input type="button" value="Click" onclick="submitForm()"/> 

當你執行代碼 -

enter image description here

+0

非常感謝你 – 2014-09-20 13:20:53