Scenario :
Need to pass an object which contains a list of sub objects to the controller.
Issue :
I'm able to get the object's value but not the value of list of sub objects inside the object.
Code :
index.cshtml
function sendData() {
var student = {
Id: 1,
Name: "xxx",
Marks: [{
Subject: "Maths",
Mark:80
},
{
Subject: "Science",
Mark: 75
}]
}
$.ajax({
url: '@Url.Action("Receive", "Home")',
data: student,
success: function (data) {
alert("done");
},
error: function (error) {
alert('error For details refer console log');
console.log(error);
}
});
}
HomeController.cs
public ActionResult Receive(Student student)
{
ViewBag.Message = "Your contact page.";
return View();
}
Student.cs
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public List<Marks> Marks { get; set; }
}
public class Marks
{
public string Subject { get; set; }
public decimal Mark { get; set; }
}
Screenshot:
Chrome debugger shows all the data were set.
but in controller i'm not getting the value of Marks
Any help would be appreciated. Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…