[英]How to Pass Complex Type to ASP.NET MVC Controller using Ajax and jQuery
I am getting null values for the complex variable( RamarksList ) properties with in another Complex type( Model ). 我在另一个复杂类型( 模型 )中获得了复杂变量( RamarksList )属性的空值。
See the values coming properly for the Name and Type but I see null values for the properties(Parameter and Comments) for the Complex Type RemarksList . 看到正确的名称和类型值,但是我看到复杂类型RemarksList的属性(参数和注释)为空值。
Here is my code. 这是我的代码。 Please take a look and advise. 请看看并提出建议。
jQuery: 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) {
}
});
Controller: 控制器:
[HttpPost]
public ActionResult Save(Model model)
{
return View();
}
Model classes: 模型类别:
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; }
}
first convert data in string using JSON.stringify 首先使用JSON.stringify以字符串形式转换数据
$.ajax({
url: url,
type: 'POST',
dataType: 'application/json',
data: JSON.stringify(data),
success: function (data) {
}
});
then change controller code from following 然后从以下更改控制器代码
[HttpPost]
public ActionResult Save(string model)
{
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
Model routes_list =(Model)json_serializer.DeserializeObject(model);
return View();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.