通过JSON将复杂对象发布到MVC Web API

时间:2019-01-21 14:38:12

标签: c# json asp.net-mvc api

我知道这是可能的,因为我一直都在使用其他API。我遇到的问题是,当我发布以下JSON时,相关子对象ICollection变为空。对于我做错了任何帮助,将不胜感激。这是我的物品:

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; }
}

这是我通过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
}]
}

最后这是我的控制器:

 // 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);
    }

1 个答案:

答案 0 :(得分:3)

您为ICollection<PartsExpressOrderItem> PartsExpressOrderItems使用的json表示形式似乎无效。它看起来应该像这样:

"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
    }
]

除非您已实现自定义序列化方案(即,需要使用PartsExpressOrderItems作为键而不是PartsExpressOrderItem),否则json表示形式和C#之间的名称必须匹配。另外,您要查看的C#属性是ICollection,因此您需要利用json数组,而不是使用相同的键来放入多个对象