我有以下域模型定義:從jquery的AJAX傳遞複雜對象行動在asp.net MVC
public class myModel {
public string Prop1 {get;set;}
public string Prop2 {get;set;}
public List<myClass> ListofStuff {get;set;}
}
public myClass {
public string Id{get;set;}
public string Name{get;set;}
}
然後我已經定義了控制器動作如下:
[HttpPost]
public ActionResult Save(MyModel someModel)
{
//do the saving
}
我請從我的JS代碼上面的動作使用jQuery AJAX
var someModel = { Prop1: "somevalue1",
Prop2: "someothervalue",
ListofStuff: [{Id: "11", Name:"Johnny"}, {Id:"22", Name:"Jamie"}]
};
$.ajax({
contentType: 'application/json, charset=utf-8',
type: "POST",
url: "/myController/Save",
data: JSON.stringify({someModel: someModel}),
cache: false,
dataType: "json",
success: function() {
alert('success!');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('error');
}
});
編輯: 當我運行上面的代碼我得到錯誤處理程序得到執行。我試圖安裝Firebug,但我的FF是版本8,它無法安裝它。所以我不確定錯誤是什麼或者怎麼看這是什麼。
我在做什麼錯?
你需要告訴我們什麼JSON.stringify(someModel)的值! –
我會編輯,謝謝 – sarsnake
你仍然可以調用服務器端的代碼,所以你應該能夠設置一個斷點在保存方法,看看模型結合是正確的,對不對? –