控制器代码:
[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();
}
});
无论出于何种原因,参数'client'始终为null,尽管检查model.selectedClient()是否正常并且ko.toJSON正在工作。
答案 0 :(得分:3)
我认为您不需要在数据中添加“客户端”填充。尝试将数据设置为: ko.toJSON(model.selectedClient())
当我的Client类看起来像这样时,'client'参数为我正确绑定了模型:
public class Client
{
public string Name { get; set; }
public string Company { get; set; }
}
...而我的ajax看起来像这样:
$.ajax({
url: "api/values/UpdateClient",
type: "post",
contentType: 'application/json',
data: "{ 'Name': 'John', 'Company': 'ABC'}"
});