0

I know this is possible because I do it all the time with other API's. The issue that I am having is that my related sub-object ICollection comes in null when I post the below JSON. 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:

{
  "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": "[email protected]",
  "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);
    }
3
  • 2
    Your json is invalid, you can't have the same property ("PartsExpressOrderItem") multiple times. Commented Jan 21, 2019 at 14:41
  • @Liam good catch... doh! I have modified accordingly, unfortunately it is still coming in null however. Commented Jan 21, 2019 at 14:51
  • 2
    @AnthonyGriggs see my answer. you need to update PartsExpressOrderItem to PartsExpressOrderItems. The json key needs to match the name of the c# property Commented Jan 21, 2019 at 14:51

1 Answer 1

3

It looks like the json representation you have for your ICollection<PartsExpressOrderItem> PartsExpressOrderItems isn't valid. 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 (i.e: needed to use PartsExpressOrderItems as the key instead of 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

Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate the quick response and help. Just an FYI I had switched to an IEnumerable with another suggestion but switched back to ICollection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.