[英]Posting Complex Objects through JSON to MVC Web API
I know this is possible because I do it all the time with other API's. 我知道这是可能的,因为我一直都在使用其他API。 The issue that I am having is that my related sub-object ICollection comes in null when I post the below JSON. 我遇到的问题是,当我发布以下JSON时,相关子对象ICollection变为空。 Any help would be greatly appreciate as to what I am doing wrong. 对于我做错了任何帮助,将不胜感激。 Here are my objects: 这是我的对象:
public class PartsExpressOrder
{
[Key]
public int OrderId { get; set; }
public int CustomerId { get; set; }
public DateTime? OrderDate { get; set; }
public double SubTotal { get; set; }
public double Freight { get; set; }
public double Taxes { get; set; }
public double Total { get; set; }
public string OrderStatus { get; set; }
public string CompanyName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string ShipToName { get; set; }
public string ShipToAddress { get; set; }
public string ShipToCity { get; set; }
public string ShipToState { get; set; }
public string ShipToZip { get; set; }
public string ShipToPhone { get; set; }
public string BillToName { get; set; }
public string BillToAddress { get; set; }
public string BillToCity { get; set; }
public string BillToState { get; set; }
public string BillToZip { get; set; }
public virtual ICollection<PartsExpressOrderItem> PartsExpressOrderItems { get; set; }
}
public class PartsExpressOrderItem
{
[Key]
public int OrderItemId { get; set; }
public int OrderId { get; set; }
public int MasterPartId { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public double Price { get; set; }
public double Total { get; set; }
}
Here is what I am posting via JSON: 这是我通过JSON发布的内容:
{
"CustomerId": 1,
"OrderDate": "2018-07-01",
"SubTotal": 65.68,
"Freight": 15.00,
"Taxes": 5.00,
"Total": 85.68,
"OrderStatus": "Sent",
"CompanyName": "Test Company A",
"FirstName": "Bob",
"LastName": "Dobalina",
"Phone": "5035551212",
"Email": "bdobalina@testcompanya.com",
"ShipToName": "Bob Dobalina",
"ShipToAddress": "5512 Test Company A Blvd. Suite 503",
"ShipToCity": "Seattle",
"ShipToState": "WA",
"ShipToZip": "98103",
"ShipToPhone": "5035551212",
"BillToName": null,
"BillToAddress": null,
"BillToCity": null,
"BillToState": null,
"BillToZip": null,
"PartsExpressOrderItem":
[{
"MasterPartId": 1,
"Description": "Widget",
"Quantity": 2,
"Price": 20.34,
"Total": 40.68
},
{
"MasterPartId": 2,
"Description": "Sprocket",
"Quantity": 5,
"Price": 5.00,
"Total": 25.00
}]
}
Lastly here is my controller: 最后是我的控制器:
// POST: api/PartsExpressOrders
[ResponseType(typeof(PartsExpressOrder))]
public IHttpActionResult PostPartsExpressOrder(PartsExpressOrder partsExpressOrder)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.PartsExpressOrder.Add(partsExpressOrder);
db.SaveChanges();
foreach(var item in partsExpressOrder.PartsExpressOrderItems)
{
item.OrderId = partsExpressOrder.OrderId;
db.PartsExpressOrderItem.Add(item);
db.SaveChanges();
}
return CreatedAtRoute("DefaultApi", new { id = partsExpressOrder.OrderId }, partsExpressOrder);
}
It looks like the json representation you have for your ICollection<PartsExpressOrderItem> PartsExpressOrderItems
isn't valid. 您的ICollection<PartsExpressOrderItem> PartsExpressOrderItems
所使用的json表示无效。 It should look something like: 它看起来应该像这样:
"PartsExpressOrderItems" : [
{
"MasterPartId": 2,
"Description": "Sprocket",
"Quantity": 5,
"Price": 5.00,
"Total": 25.00
},
{
"MasterPartId": 3,
"Description": "Something",
"Quantity": 10,
"Price": 12.00,
"Total": 47.00
}
]
The names between json representation and C# need to match unless you have a custom serialization scheme implemented (ie: needed to use PartsExpressOrderItems
as the key instead of PartsExpressOrderItem
). 除非您实现了自定义序列化方案,否则json表示形式和C#之间的名称必须匹配(即:需要使用PartsExpressOrderItems
作为键而不是PartsExpressOrderItem
)。 Also, you're C# property you're looking at is an ICollection
so you need to utilize a json array instead of putting in multiple objects with the same key 另外,您正在查看的C#属性是一个ICollection
因此您需要利用json数组,而不是使用相同的键放入多个对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.