I'm creating a webservice where one can post a new order with multiple lines.
Models
public class Order {
public int OrderID { get; set; }
public string Description { get; set; }
public string Account { get; set; }
public ICollection<OrderLine> OrderLine { get; set; }
}
public class OrderLine {
public int OrderLineID { get; set; }
public int OrderID { get; set; }
public string Product { get; set; }
public double Price { get; set; }
}
Controller
public class OrderController : ApiController
{
[HttpPost]
public string Create(Order order)
{
OrderRepository or = new OrderRepository();
return "Foo";
}
}
With Postman I create a post request in Json like this:
{"Description" : "Abc", "Account" : "MyAccount",
"OrderLine[0]" : {
"ItemCode": "Item1",
"Price" : "10"
}
}
When I run the debugger in Visual Studio, The Order model is populated from the request, but OrderLine is NULL. When I change
public ICollection<OrderLine> OrderLine {get; set;}
to
public OrderLine OrderLine {get; set;}
And my Json string in Postman to
{"Description" : "Abc", "YourReference" : "yourABC", "Account" : "AB1010",
"OrderLine" : {
"ItemCode": "Item1",
"Price" : "10"
}
}
My model gets populated when I post the data. I want to post a collection of OrderLines. What am I doing wrong?