【发布时间】:2016-09-06 18:03:38
【问题描述】:
这是我的模型
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; }
}
这是我的 WebApi 方法
[HttpPost]
public void SetStudentInfo(Student student)
{
...
}
这是我从 JS 打来的电话(这个有效)
$.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);
}
});
这是我从 JS 打来的电话(这个不行)
$.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);
}
});
当我发送第二个请求时,WebApi 方法上的整个学生为空;这与添加嵌套对象有关,但我不确定原因或如何解决此问题。
我尝试过对数据进行字符串化,但这也不起作用。
【问题讨论】:
-
您是否尝试过课程:[ ]?
-
@tmg 是的,同样的问题
-
试试
data: { "FirstName": "John", "LastName": "Smith" },
标签: c# ajax asp.net-web-api