Here are my models
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Course> Courses { get; set; }
}
public class Course
{
public string Name { get; set; }
public string Details { get; set; }
}
Here is my WebApi method
[HttpPost]
public void SetStudentInfo(Student student)
{
...
}
Here is my call from JS (This one works)
$.ajax({
async: false,
type: "POST",
url: "http://localhost:50067/api/Students/SetStudentInfo",
data: {
FirstName: "John",
LastName: "Smith"
},
success: function (xhr) {
console.log(xhr);
},
error: function (e) {
console.log(e);
}
});
Here is my call from JS (This one DOES NOT work)
$.ajax({
async: false,
type: "POST",
url: "http://localhost:50067/api/Students/SetStudentInfo",
data: {
FirstName: "John",
LastName: "Smith",
Courses: null
},
success: function (xhr) {
console.log(xhr);
},
error: function (e) {
console.log(e);
}
});
When I send up the second request, the entire student on the WebApi method is null; it's something to do with adding in nested objects but I'm not sure why, or how to fix this.
I've tried stringifying the data, but that doesn't work either.