24sharon
24sharon

Reputation: 1975

Pass Array of object from angular $http to mvc web api via post method

I try to pass an array of object from angular to mvc webapi via post method but with no success.

This is my client code:

    $http({
        url: '/api/messages/graph',
        data: { users: siteService.siteObject.users  },
        method: 'Post'
    })

This is my mvc web api controller (Try with [FromBody] attribute and without)

   [Authorize(Roles = "admin")]
    [HttpPost]
    public List<graph_item> graph([FromBody] DaganUser[] users)
    {
     ...
    }

In the browser console seems that the data pass to server, But the controller parameter always null

enter image description here

Upvotes: 1

Views: 4308

Answers (2)

24sharon
24sharon

Reputation: 1975

Anik Islam Abhi You give me the direction to the solution, and instead of passing

 data: { users: siteService.siteObject.users  },

I wrote

  $http({
                url: '/api/messages/graph',
                data:  siteService.siteObject.users  ,
                method: 'Post'
            })

And now it work Thanks

Upvotes: 0

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

You are passing object

Convert this

public List<graph_item> graph([FromBody] DaganUser[] users)

to this

public List<graph_item> graph([FromBody] DaganUser users)

And DaganUser should have the property name users as you are passing object with property users

Upvotes: 2

Related Questions