Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
up vote 3 down vote accepted

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)
share|improve this answer
    
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. – Kautzmann Nov 12 '13 at 15:32
1  
Right using dynamic also works. – Chandermani Nov 12 '13 at 15:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.