作为标题,我有一个问题,我看到我的对象数组在后端是空的。例如,我在后端定义了2个类…
public class ScoreModel {
public string Subject { get; set; }
public float Score { get; set; }
}
public class StudentTestModel
{
public string Name { get; set; }
public ScoreModel[] Scores { get; set; }
}
在上面显示的代码中,类StudentTestModel
有一个包含2个简单数据成员的ScoreModel
数组。然后在前端,我使用jQuery库像这样调用后端....
$("#btnCountScore").click(function () {
var frm = new FormData();
frm.append("Name", "Garfield");
var r = [
{ "Subject": "Chinese", "Score": 89 },
{ "Subject": "English", "Score": 87 },
{ "Subject": "Math", "Score": 76 },
{ "Subject": "NaturalScience", "Score": 90 },
{ "Subject": "SocialScience", "Score": 83 },
];
var r_form = [];
for (var i = 0; i < r.length; i += 1) {
var f = new FormData();
f.append("Subject", r[i].Subject);
f.append("Score", r[i].Score);
r_form.push(f);
}
frm.append("Scores", r_form);
$.ajax({
url: "../Home/CountScore",
method: "post",
processData: false,
contentType: false,
data: frm,
success: function (ret) {
$("#lblShowAverage").text(ret);
}
});
});
当我调试时,我可以看到Name
包含值,但Scoures
是一个空数组在我的后端,然后我无法得到我想要的。有人可以指导我一个方法来纠正这个吗?
您是否尝试在发送之前将对象转换为json,然后在后端反序列化?
前端:JSON.stringify(农场)
后台:JsonSerializer.Deserialize (jsonString);