Posting array of objects with MVC Web API

前端 未结 3 532
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 15:44

I have a basic post operation that works on a single object of RecordIem. What I would like to do is do the same action but in bulk by posting an array of reque

相关标签:
3条回答
  • 2020-12-02 16:11

    It's important that your json contains the request parameter name. A other note: you have to post it as an array.

    Your json would look like this:

    {
        "request": [
            {
                "Id": "...",
                "System": 1,
                ...
            },
            { ... }
        ]
    }
    
    0 讨论(0)
  • 2020-12-02 16:20

    Since your Post expects an RecordItem[], your JSON content in your request body should be in an array as well.

    What you have is pretty close -- try adding a pair of square bracket [] around your data:

    [{
        Id : "7UP24fVkGOxSjrcclghe_mP2-po",
        System : 1,
        Environment : "Production"
    },
    {
        Id : "ClPE188H4TeD2LbQPeV_EzCsKVM",
        System : 1,
        Environment : "Production",
        Label : "RestTest1"
    },
    {
        Id : "SAWTMJzm-_AFqoNw70-gLeUzB4k",
        System : 1,
        Environment : "Production"
    }]
    
    0 讨论(0)
  • 2020-12-02 16:22

    For all that just get an empty array whatever they try, try this:

    var request = $.ajax({
      dataType: "json",
      url: "/api/users",
      method: "POST",
      data: { '': postData}
    });
    

    The data must be a single anonymous object instead of a raw array.

    Info was found here.

    0 讨论(0)
提交回复
热议问题