Search code examples
c#asp.net-web-apiodataasp.net-web-api2

OData V4 client pass pass parent and child as parameter


I have an OData V4 controller for "Orders" and inside the orders controller I have added a method called "SaveWithChild" that is a HttpPost with parameter of type ODataActionParameters.
In the WebApiConfig I have added an ActionConfiguration for the "SaveWithChild" on the "Orders" entity with a parameter type of "Order".

Now let's assume the "Order" model has a property called "UpdateUser" and also has a child property called "OrderLine" and the OrderLine has a property called "UpdateUser".

Like the following:

class Order
{
    public string UpdateUser { get; set; }
    public OrderLine OrderLine { get; set; }
}

class OrderLine
{
    public string UpdateUser { get; set; }
}

Now on the client side I would like to be able to update the "UpdateUser" field for both the parent "Order" and the child "OrderLine" by calling the "SaveWithChild" method on the controller.

var context = new Container("URI");
var order = (from o in context.Orders.Expand("OrderLine") where o.ID = 1 select o).FirstOrDefault();
order.UpdateUser = "test";
order.OrderLine.UpdateUser = "test";
context.Orders.SaveWithChild(order);

When I run Fiddler I can see that the "OrderLine" object is not being passed with the Order object in the JSON http post.

I can serialize the object into a string, pass it as a string to the controller, and then deserialize it inside the control.
That does work but I am hoping there is a better way.


Solution

  • You can do something like this: DataServiceCollection coll = new DataServiceCollection(context.People.Expand("Photo").Where(p => p.UserName == "russellwhyte")); coll.First().Photo.Name = "test"; coll.First().LastName = "test";
    context.SaveChanges(); Let me know if this does not resolve your issue.