Jquery 将对象列表发布到web api方法

Jquery 将对象列表发布到web api方法,jquery,post,asp.net-web-api,Jquery,Post,Asp.net Web Api,您好,我想通过以下方式将objectos列表发布到web api方法: var uri = "http://localhost:" + port + "/api/Account"; $.ajax({ dateType: "json", method: "POST", url: uri, data: JSON.stringify(schedule), success: function (result) { alert(resu

您好,我想通过以下方式将objectos列表发布到web api方法:

var uri = "http://localhost:" + port + "/api/Account";
$.ajax({
    dateType: "json",       
    method: "POST",
    url: uri,
    data: JSON.stringify(schedule),
    success: function (result) {
        alert(result);
    },
    error: function () {
        alert("mal")
    }
})
我的web api方法如下所示:

public IHttpActionResult createSchedule([FromBody]List<Schedule> schedule)
{
        return Ok(schedule.Capacity);
}
public IHttpActionResult createSchedule([FromBody]列表计划)
{
返回Ok(计划。容量);
}

问题是返回的是0,而不是列表中的对象数。我想那是因为名单上没有人。我做了什么坏事?。我读了一些关于将数据设置为
date:{“”:schedule}
的内容,但它不起作用。它抛出了一个空引用异常。

webapi方法将查找
schedule
对象


因此,将您的
data:schedule,
更改为
data:{schedule:schedule},

您正确地传递了数据,但是您需要指出请求的内容类型是JSON(
$.ajax
中的
数据类型
选项告诉jQuery对响应的预期)。将
contentType
参数添加到ajax调用中,它应该可以工作:

var uri = "http://localhost:" + port + "/api/Account";
var schedule = [{}, {}, {}];
$.ajax({
    dateType: "json",
    contentType: "application/json",
    method: "POST",
    url: uri,
    data: JSON.stringify(schedule),
    success: function (result) {
        alert(result);
    },
    error: function () {
        alert("mal")
    }
})

我认为错误在web api方法中,因为我正在使用postman(chrome)进行测试,当我尝试发送此json时,会出现空引用异常:

var schedule = [{day: "Lunes", hour: "9-11", id_flat: 1},{day: "Martes", hour: "15-16", id_flat: 1}]

这不是问题所在,因为该方法运行良好。否则就不行了。不管怎么说,我刚刚试过,它仍然返回0。嗯。。
schedule
看起来像
[{},{}..]
吗?是的,看起来像你说的。