Reputation: 2971
Dead simple question, possibly not so simple answer. Posting JSON.
public void Post(Model1 model1, Model2 model2)
{}
model1 is populated but not model2 (null).
public void Post(Model2 model2, Model1 model1)
{}
Now, model2 is populated but not model1 (null).
Why?
Edit
The reason for two parameters? Model2 used to be referenced from Model1, but that didn't work. That's when I split them up.
Edit
Right. Thanks marcind for the answer to the question above. Now for the reason the original setup didn't work. I'm not the forms universe anymore. I post Json. If you have child objects in your model then post child objects in your json.
Given
class ProductEditModel {
public string Name {get; set;}
}
class UserEditModel {
public string User {get; set;}
public ProductEditModel Product {get; set;}
}
the following
{"user": "philip", "product.name": "barbie"}
is not going to work. You'd even get an error if you in js try and setup the sematic equivalent
{user: "philip", product.name: "barbie"}
Neither of the following work either, I don't know why they would:
{"user": "philip", "productname": "barbie"}
{"user": "philip", "product_name": "barbie"}
What does work and which should be obvious holding my profession is
{"user": "philip", "product": {"name": "barbie"}}
Please kick me.
Beware! The following will not work given corresponding edit to the model above.
{"user": "philip", "ProductEditModel": {"name": "barbie"}}
Upvotes: 2
Views: 376
Reputation: 53183
Not sure which version you are using, but the general rule we have settled on is that when binding complex types Web API considers the entire body of the request to represent a single entity and thus a single action parameter. In your case if you want to bind multiple Model
s you could introduce a custom binding object or alternatively you could bind to a Model[]
or some other collection type.
Upvotes: 2