2012-10-09 16 views
1

控制器代碼:的WebAPI傳遞的數據是空

[HttpPost] 
public void UpdateClient(Client client) 
{ 
    // Rest of code 
} 

客戶端代碼:

$.ajax({ 
      url: "api/client/UpdateClient", 
      type: 'post', 
      contentType: 'application/json', 
      data: "{client: " + ko.toJSON(model.selectedClient()) + "}", 
      success: function (result) { 
       getClients(); 
       $("#loader").hide(); 
      }, 
      failure: function (result) { 
       alert(result.d); 
       $("#loader").hide(); 
      }, 
      error: function (XMLHttpRequest, textStatus, errorThrown) { 
       alert("An error occurred, please try again."); 
       $("#loader").hide(); 
      } 
     }); 

無論出於何種原因,參數「客戶」始終是空儘管檢查model.selectedClient()是確定和ko.toJSON正在工作。

回答

3

我不認爲你需要添加'客戶'填充到您的數據。嘗試數據設置爲: ko.toJSON(model.selectedClient())

的「客戶」參數得到了我正確綁定模型時,我的客戶端類如下所示:

public class Client 
{ 
    public string Name { get; set; } 
    public string Company { get; set; } 
} 

...和我的阿賈克斯看起來像這樣:

 $.ajax({ 
      url: "api/values/UpdateClient", 
      type: "post", 
      contentType: 'application/json', 
      data: "{ 'Name': 'John', 'Company': 'ABC'}" 
     }); 
+0

完美,只是改變了那個伎倆。 – user1166905