0

I want to know if passing a collection to the post of an ODataController is even possible before I waste my time trying to figure out where my problem is.

I've seen variations of this question over the internet, but nothing that has led me to an answer.

I created the Post method on an ODataContoller where I want to pass in a IEnumerable of a complex object. However, when I debug the controller, the parameter is null.

When I take each individual element of the collection and pass it into a Controller's Post (that takes a single object), the object is accepted. So I know the individual objects are being formatted correctly.

Chris
  • 1,690
  • 2
  • 17
  • 24

1 Answers1

1

Technically this is possible. If you get null as complexObjects in the following call

[HttpPost]
public IHttpActionResult CreateMany([FromBody] IEnumerable<ComplexObject> complexObjects)
{
  // ...
}

it's probably due to a format error in the bodies json object. If you have a working single object you can post, you just need to wrap it in brackets ... an array is an array even if it cotains only one element. This assumes, you check your web api actions via postman, fiddler etc., where you can 'compose' the entire request. Alternatively you can use the output of a 'GET all' action (if you have one) as input.

Regarding another aspect, the REST-fulness of list creation, you may find RESTful way to create multiple items in one request interesting

Hope this helps.

Community
  • 1
  • 1
Jürgen Röhr
  • 886
  • 6
  • 10
  • Rohr, thank you for the link. It was very useful. I have been testing as you suggested, but nothing seems to be working. I have found that I can pass in an Array with the APIController. However, not the ODatacontroller – Chris Nov 19 '16 at 04:42