【发布时间】:2016-02-09 22:19:21
【问题描述】:
我在另一个复杂类型 (Model) 中获取复杂变量 (RamarksList) 属性的空值。
查看 Name 和 Type 的正确值,但我看到复杂类型 RemarksList 的属性(参数和注释)的值为空强> .
这是我的代码。请看看并提出建议。
jQuery:
var data = {
'Name': 'Apple',
'Type': 'Fruit',
'RemarksList': [
{
'Parameter': 'test 1',
'Comments': 'test 123'
},
{
'Parameter': 'test 2',
'Comments': 'abc 3455'
}
]
};
var url = "/Controller/Action";
$.ajax({
url: url,
type: 'POST',
dataType: 'application/json',
data: data,
success: function (data) {
}
});
控制器:
[HttpPost]
public ActionResult Save(Model model)
{
return View();
}
模型类:
public class Model
{
public int Id { get; set; }
public string Name { get; set; }
public string Type{ get; set; }
public List<Remarks> RemarksList { get; set; }
}
public class Remarks
{
public int Id { get; set; }
public string Parameter { get; set; }
public string Comments { get; set; }
}
【问题讨论】:
-
data: JSON.stringify(data), -
试过了。有了这个,我得到了所有的东西,包括类型和名称..不知道为什么..
-
对不起,你还需要
contentType: 'application/json; charset=utf-8' -
完美。谢谢你。它正在工作..我是否需要同时设置 dataType: 'application/json', contentType: 'application/json; charset=utf-8',?
-
contentType用于发送给控制器的数据,dataType用于服务器发送回客户端的数据
标签: c# jquery asp.net ajax asp.net-mvc