c#webapi没有收到JS Array

时间:2015-01-12 15:46:16

标签: c# .net ajax

我有一个测试js函数,它应该通过Post方法将数据发布到webapi

function test() {
    var puffs = [];
    var puffObject = {
        id: "2735943",
        priority: "1"
    };

    puffs.push(puffObject);
    puffs.push(puffObject);

    var dto =
    {
        po: puffs
    };
    $.ajax({
        type: "POST",
        url: "../api/PuffPriority/",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(dto),
        dataType: "json",
        success: function (data, status) {
            if (data) {
                console.log("Welcome");
            } else {
                console.log("No data");
            }
        },
        error: function (error) {
            console.log("Error");
        }
    });
}

在控制器类中我有

public void Post(PuffPriority[] po){
//Do something here with po, but po is always empty
}

其中PuffPriority模型是

public class PuffPriority
{
    public string id { get; set; }
    public string priority { get; set; }
}

我不知道什么是错的,价值是由JS发送的,但api控制器不会取得它:(请帮忙,我已经浪费了很多时间。

2 个答案:

答案 0 :(得分:0)

您的代码中存在错误。 只需更改

url: "../api/PuffPriority/"

url: "../api/Post/"

或将您的方法名称从Post更改为PuffPriority

答案 1 :(得分:0)

已更改

public void Post(PuffPriority[] po){ 
//Do something here with po, but po is always empty 
}

[FromBody]List<PuffPriority> po{
//Do something here with po, but po is always empty
}