As title, I have a problem with this that I saw my array of the object is empty at the back end. For example, I've defined 2 classes at the back end...
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; }
}
At the code shown upper, the classStudentTestModel has an array of ScoreModel that contains 2 simple data members. Then at the front end, I use jQuery library to call back end like this....
$("#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);
}
});
});
When I debug, I can see the Name contains value but Scoures is an empty array at my back end, then I couldn't get what I want. Could someone guide me a way to correct this?
Have you tried to make the object into json before sending it then deserialize it in the backend?
FrontEnd: JSON.stringify(frm)
Backend: JsonSerializer.Deserialize(jsonString);