Reputation: 953
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?
Upvotes: 3
Views: 480
Reputation: 35587
You're POSTing an array of OrderLine
so your request needs to contain an array:
"OrderLine" : [{}, {}]
and it should look like this:
{"Description" : "Abc", "YourReference" : "yourABC", "Account" : "AB1010",
"OrderLine" : [{
"ItemCode": "Item1",
"Price" : "10"
},
{
"ItemCode": "Item2",
"Price" : "20"
}]
}
Upvotes: 1