【发布时间】:2011-11-18 00:03:51
【问题描述】:
我定义了以下领域模型:
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
}
我使用 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 版,无法安装。所以我不确定错误是什么或如何查看它是什么。
我做错了什么?
【问题讨论】:
-
你需要告诉我们 JSON.stringify(someModel) 的值是什么!
-
你仍然调用服务器端代码,所以你应该可以在 Save 方法中设置一个断点来查看模型绑定是否正确,对吧?
-
我做到了。它永远不会达到那个断点。只是出错了。
-
完全删除 listOfStuff 后,一切都像魅力一样。
标签: asp.net-mvc-3 jquery