Reputation: 13561
When my parent class/model is posted back to the server and I use the BindAttribute "Bind", child objects are all null.
If I don't include the binding, then it works
public ActionResult SaveEdit(int id, Person person)
For instance the person.Address property is not null, and it's properties are set based upon the form submission.
Just FYI, Address is created in the view using a EditorFor, everything is included in the request form.
But if I use Bind, it doesn't work..
public ActionResult SaveEdit(int id, [Bind(Include = "PersonId, AddressId, Address.AddressId, Address.Line1, Address.City, Address.State, Address.Zip")] Person person)
person.PersonId and person.AddressId are set correctly, but person.Address is null.
If use Bind and multiple parameters, like so...
public ActionResult SaveEdit(int id, [Bind(Include = "PersonId")] Person person,
[Bind(Prefix = "Address", Include = "AddressId, Line1, City, State, Zip")] Address address)
It halfway works, both object are created, neither has related properties that would normally be there.
Is this normal behavior, or have I done something wrong?
Upvotes: 2
Views: 2121
Reputation:
You have not shown your model, but assuming it contains properties PersonId
, AddressId
and Address
where Address
is a complex object then it needs to be
public ActionResult SaveEdit(int id, [Bind(Include = "PersonId, AddressId, Address")] Person person)
However, the better approach is to use a view model containing only those properties you are editing and avoid the use of [Bind(Include = "properties, list")]
Upvotes: 4