Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

73
Views
Javascript ajax post a complex object with an array of complex objects inside

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!