The code is setup through an ASP.NET MVC Post through the controller. The controller method handling the post is:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(
[Bind(Prefix = "agent")] AgentEntity agent,
[Bind(Prefix = "address")] AddressEntity address,
[Bind(Prefix = "personAddress")] PersonAddressEntity personAddress,
[Bind(Prefix = "email")] EmailEntity email,
[Bind(Prefix = "personEmail")] PersonEmailEntity personEmail,
[Bind(Prefix = "phone")] PhoneEntity phone,
[Bind(Prefix = "personPhone")] PersonPhoneEntity personPhone)
{
personAddress.Address = address;
agent.PersonAddress[0] = personAddress;
personEmail.Email = email;
agent.PersonEmail[0].EmailTypeId = personEmail.EmailTypeId;
personPhone.Phone = phone;
agent.PersonPhone[0] = personPhone;
if (ModelState.IsValid)
{
agent.Save(true);
return RedirectToAction("List");
}
ViewData["AddressTypes"] = SelectListFactory.CreateSelectList(
QueryService.FindAddressTypes(), personAddress.AddressTypeId, Resources.DropDownGeneric);
ViewData["EmailTypes"] = SelectListFactory.CreateSelectList(
QueryService.FindEmailTypes(), personEmail.EmailTypeId, Resources.DropDownGeneric);
ViewData["PhoneTypes"] = SelectListFactory.CreateSelectList(
QueryService.FindPhoneTypes(), personPhone.PhoneTypeId, Resources.DropDownGeneric);
ViewData["States"] = SelectListFactory.CreateSelectList(QueryService.FindStates(), address.StateId, Resources.DropDownGeneric);
return View(agent);
}
What you don't see is when the agent object is created through the default binder, it is assigned the primary key representing the existing parent record and the IsNew field is set to false. All other objects are assigned values from the ASP.NET form.
Agent is a subtype of Person and Person has a many to many relationship with Address, Email and Phone.
I'm able to update fields on agent that result in updates. But the code above inserts new child records rather than update them...