0

I'm building a comma separated string and passing it to my WebAPI method like so:

var projectids="";
 for (var i = 0; i < chk.length; i++) {
 projectids += chk[i].VMIProjectId + ",";
 }
 //projectids = "1,2,3"
 $.ajax({
 type: 'POST',
 url: "http://localhost:52555/device/6/AddProjectsToDevice",
 contentType: 'application/json; charset=utf-8',
 dataType: 'json',
 data: JSON.stringify(projectids),
 success: function (msg) {
 },
 error: function (data) {
 debugger;
 }
 });

I 'm reaching the WebApi method successfully, but my IEnumerable array projectIds is always null. Here's the method:

 [HttpPost]
 [Route("device/{deviceId}/AddProjectsToDevice")]
 public IEnumerable<VMI_DeviceLinkedProject> AddProjectsToDevice([FromUri]long deviceId,[FromBody] IEnumerable<long> projectIds){}

How do i pass my comma separated list of ids to my WebAPI method? Thanks for reading

asked May 13, 2015 at 15:52

2 Answers 2

3

Right now your controller action is getting a comma separated string of numbers and not a JSON array ("[1,2,3,4]"), which is why the model binder isn't working:

var projectids = [];
for (var i = 0; i < chk.length; i++) {
 projectids.push(chk[i].VMIProjectId);
}

And then the stringified array should bind to the IEnumerable<long> properly.

answered May 13, 2015 at 16:06

Comments

1

Instead of passing it as a csv value you can pass it as an array using the same code as below,

var projectIdList =[];
for (var i = 0; i < chk.length; i++) {
 projectIdList.push(chk[i].VMIProjectId);
}

Similarly change the parameter name in api method also as projectIdList and pass it.

answered May 13, 2015 at 16:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.