我定义了以下域模型:
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;}
}
然后我将 Controller 操作定义如下:
[HttpPost]
public ActionResult Save(MyModel someModel )
{
//do the saving
}
我使用 jquery ajax 从 JS 代码调用上述操作
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,无法安装。所以我不确定错误是什么或如何查看它是什么。
我做错了什么?
最佳答案
我解决了这个问题。 MyClass 需要有一个无参数构造函数才能使绑定(bind)正常工作。
关于asp.net-mvc-3 - 从 jquery ajax 将复杂对象传递给 asp.net MVC 中的 Action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8176144/