【发布时间】:2013-10-14 13:46:38
【问题描述】:
我有以下 2 个保存数据的类:
public class ItemList{
public IList<Item> Items{get;set;}
}
public class Item{
public int id {get;set}
public string name {get;set}
}
我的控制器看起来像:
public virtual JsonResult SaveItems(ItemList items)
{}
我尝试像这样发布一个 JS 对象:
var toPost = { "items" : [ {"id" : 1, "name":"test}, {"id" : 1, "name":"test"}] }
$.ajax({
type: "POST",
url: "URL TO POST TO",
dataType: "json",
data: toPost,
traditional: true,
success: function (data, status, request) {
if (data.Error != undefined) {
alert("System Error: " + data.Error);
return;
}
console.log("Success");
},
error: function (request, status, error) {
console.log("ERROR");
}
});
我在发布之前做了一个console.log,数据看起来与toPost 变量中描述的一样,但是在C# 端调试时ItemList items 为空
【问题讨论】:
-
尝试大写 I 的项目(在你的班级和 json 字符串中不同)
-
内容类型属性在哪里?
-
它的数据类型:“json”我使用
-
数据类型用于响应。你需要内容类型
-
Items中的I需要大写,你需要_contentType属性。
标签: c# javascript json null