1

Is it possible to retrieve a list of entities as a parameter for an action in c# odata?

Tried the following but it casues the entire controller to not work:

[HttpPost]
[NhSessionManagement()]
[ODataRoute("BatchUpdate")]
public async Task<IHttpActionResult> BatchUpdate(List<Item> items, bool updateDefaultJSONFile)
{
    return Ok();
}

This is the config:

        {
            var action = builder.EntityType<Item>().Collection.Action("BatchUpdate");

            action.CollectionParameter<Item>("items");
            action.Parameter<bool>("updateDefaultJSONFile");
        }

This is how I then send the data:

$http.post(appConfig.serviceUrl + "api/items/Service.BatchUpdate", { items: itemsToUpdate, updateDefaultJSONFile: true}).success(function (data, status, headers, config) {
    debug("Successfully saved");
}).error(function (data, status, headers, config) {
    debug("Failed to save");
});

In fiddler:

POST http://192.168.20.108/api/items/Service.BatchUpdate HTTP/1.1
Host: 192.168.20.108
Connection: keep-alive
Content-Length: 83114
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
Origin: http://192.168.20.108
Authorization: Bearer ****
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://192.168.20.108/
Accept-Encoding: gzip, deflate
Accept-Language: sv-SE,sv;q=0.9,en-US;q=0.8,en;q=0.7

{"items":[{"Key":"helpful","Text":"test"}],"updateDefaultJSONFile":false}

1 Answer 1

1

Specify collection parameters as an array, not a list:

[HttpPost]
[NhSessionManagement()]
[ODataRoute("BatchUpdate")]
public async Task<IHttpActionResult> BatchUpdate(Item[] items, bool updateDefaultJSONFile)
{
    return Ok();
}

Config is correct, good job.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. Now the controller seems to be working. But his action is not. I get a 404 error with these paramaters, without any parameters my call is working so its probably a missmatch with how I send and recieve the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.