6

I am trying to pass an array of int but I can not get the value in the webapi method

var postData = { "deletedIds": deletedIds };

    $.ajax({
        type: "DELETE",
        traditional: true,
        dataType: 'json',
        contentType: 'application/json',
        cache: false,
        url: "/api/Management/Models",
        data: JSON.stringify(postData),
        success: ModelDeleted,
        error: ModelNotDeleted
    });

and in apiController :

[HttpDelete]
        public bool DeleteModel(int[] deletedIds)
        {
            return modelsRepository.DeleteModels(deletedIds);
        }
Muhammad Alaa
  • 608
  • 1
  • 10
  • 15

3 Answers3

9

Your code looking pretty Ok to me. Please define structure of "deletedIds" object. one suggestion is to Use new Array() object to initialize deletedIds property and remove JSON.stringify() . A similar question asked here.

EDIT

Web API supports parsing content data in a variety of ways, but it does not deal with multiple posted content values. A solution for your problem could be to create a ViewModel with a property of int[] type. Like code below,

public class SimpleViewModel
{
    public int[] deletedIds{ get; set; }
}

//Controller
[HttpDelete]
    public bool DeleteModel(SimpleViewModel deletedIds)
    {
        return modelsRepository.DeleteModels(deletedIds.deletedIds);
    }

and use it as parameter type.

Community
  • 1
  • 1
Shashank
  • 471
  • 6
  • 19
  • I tried to use new Array but still get null on webapi method parameter – Muhammad Alaa May 04 '13 at 14:45
  • Thanks for your reply but the question you referenced is not the same my code is working fine when I use action method in regular controller but when url is to webapi it can not get the data – Muhammad Alaa May 04 '13 at 14:48
  • Use firebug to test post data. – Shashank May 04 '13 at 14:51
  • the data is ok on client side but the problem is webapi uses different model binding than mvc – Muhammad Alaa May 04 '13 at 15:06
  • 2
    decorating your action parameter with the [FromUri] attribute. [this](http://stackoverflow.com/questions/14628576/passing-an-json-array-to-mvc-web-api-via-get) post may help you. – Shashank May 04 '13 at 15:14
2

At last, based on @Shashank Answer it worked and the code modified as :

var deletedIds = new Array();

deletedIds.push(modelId);

var postData = { "DeletedIds": deletedIds };
$.ajax({
    type: "Delete",
    traditional: true,
    dataType: 'json',
    cache: false,
    url: "/api/Management/Models",
    data: postData,
    success: ModelDeleted,
    error: ModelNotDeleted
});

and the apiController :

[HttpDelete]
        public bool DeleteModels(DeleteViewModel dvm)
        {
            return modelsRepository.DeleteModels(dvm.DeletedIds);
        }

and for the DeleteViewModel :

public class DeleteViewModel
    {
        public int[] DeletedIds { get; set; }
    }
Muhammad Alaa
  • 608
  • 1
  • 10
  • 15
0

I suspect that the stringify bit is messing up your object - have you tried assigning it to a variable to see if it's the expected formatting?

You're stringifying the whole thing at the moment:

{ "deletedIds": deletedIds }

while what you probably want to send as post body is this:

{ "deletedIds": JSON.stringify(deletedIds) }

Joanna Derks
  • 4,033
  • 3
  • 26
  • 32