I have the following method signature on a WebAPI endpoint:
[HttpPost]
public async Task<IHttpActionResult> AddShopping(List<ShoppingList> shoppingList)
{
This is example JSON received in the POST body:
[
{
"name":"Bob",
"items":{"food":"apple","quantity":1}
},
{
"name":"Tim",
"items":[{"food":"orange","quantity":3}]
},
{
"name":"John",
"items":[
{"food":"banana","quantity":3},
{"drink":"coke","quantity":2}
]
}
]
Notice the first 'items' is an object and the second and third 'items' are arrays.
I want to bind it to this model, so the object in the first 'items' is put into the 'Items' List. If this is possible, how do I do it? Hopefully it's a matter of decorating the 'Items' property in the model with an attribute!
namespace Test
{
using System.Collections.Generic;
public class ShoppingList
{
public List<Item> Items { get; set; }
public string Name { get; set; }
}
public class Item
{
public string Food { get; set; }
public int Quantity { get; set; }
}
}