0

I'm using AngularJS $http to send data to my Web API server. I don't know why, but my JS Array is not going to the server side, it's going NULL, the id is going OK.

This is what I have:

Client-Side:

var dataArray = [
     {
        Prop1: "4674279"
     }
];

var id = 1;

$http({
    method : 'POST',
    url : 'http://localhost/Services/Controller/Method',
    auth : true,
    data: {
        'id' : id,
        'items' : dataArray 
    },
    headers: {
           "Content-Type": "application/json"
    }})
    .success(function (data, status, headers, config) {
         if (status === 200) {
              return data;
         }
     })
     .error(function (data, status, headers, config) {
           console.log('[ERROR] Status Code:' + status);
    });

Server-Side:

public partial class Item
{
     public string Prop1 { get; set; }
}

public ReturnType Method(int id, List<Item> items)
{

}

What I'm doing wrong? I've tried JSON.stringify and so on, but wont works.

1 Answer 1

3

You cannot do post to two parameter on the controller method. The standard mediatypeformatter would format to a single object

Your options are to

Create a class such as

public class ListItems {
  public int id {get;set;}
  public List<Items> Items {get;set;}
}

Or make the id as part of url such as

http://localhost/Services/Controller/Method/:id

Then the api method would look like

public ReturnType Method(int id, [FromBody]List<Item> items)
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah right, I solve it diff, changing the controller for a dynamic, such as: public dynamic Method(dynamic requestObj) And then process the JSON data to the Properties. This solves. Thanks.

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.