0

I have the following code. Why can't I have an array or list as inparameter directly? It works fine with an array or list inside a class like below.

Request payload:

{"ids":[1,2,4]}

Angular:

send: {
    method: 'POST',
    url: controllerPath + '/send',
    isArray: true
}

agreementResource.send({ ids: vm.agreementIds }).$promise.then(function (data) {

});

C#:

//Works
[HttpPost]
[Route("send")]
[Authorize(Roles = "Admin")]
public IHttpActionResult Send(AgreementIdsViewModel ids)
{
    foreach (var id in ids.Ids)
    {

    }
}


//Does not work
[HttpPost]
[Route("send")]
[Authorize(Roles = "Admin")]
public IHttpActionResult Send(int[] ids)
{
    foreach (var id in ids)
    {

    }
}

public class AgreementIdsViewModel
{
    public int[] Ids { get; set; }
}

I know it can be done via GET-request so I think it should be possible.

GetSend([FromUri] int[] ids)

And send request:

/Send?ids=1&ids=2&ids=3 
3
  • Use public IHttpActionResult Send([FromBody] int[] ids) Commented Nov 18, 2016 at 14:56
  • I agree with @Fabio and also you can set a break point inside your controller method and go through everything in this like the Request.Form.AllKeys Commented Nov 18, 2016 at 14:58
  • This did unfortunately not work Commented Nov 18, 2016 at 15:16

1 Answer 1

0

please follow the following:

In angular service:

send: {
    method: 'POST',
    url: controllerPath + '/send',
   }

the big problem exists in your payload. it should look like the following:

[1,2,3,4]

without adding ids. if you want to make it like this you can use lodash or underscore like the following:

 var ids = [];

 _.map(file, function (item, index) {
                ids.push(item.name);
            });

your Web Api doesn't have any problem, and it's correct:

[HttpPost]
[Route("send")]
[Authorize(Roles = "Admin")]
public IHttpActionResult Send(int[] ids)
{
    foreach (var id in ids)
    {

    }
}

and your angular controller where you will call the service looks like this:

agreementResource.send({ ids }).$promise.then(function (data) {}

Tell me if it is worked for you.

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

2 Comments

I don't want to add a library like lodash or underscore to make this work.
it is an important issue. it just to make it easy for you. The key point is that your payload should look like this: [1,2,3,4] and not like this {"ids":[1,2,4]}

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.