I have 2 related entities: Contact
and Address
. There is a relation between each other. 1 Contact with 1 Address.
In the create Form, I fill the FirstName and LastName text boxes. I have a field related to the address.
In some situations, I perform an AJAX request to fill automatically the Address Info.
The problem is: when I click save, that creates a new Address entry instead of just associating the contact with the AddressID provide by the AJAX request.
The desired behaviour is that if the AddressID is empty save a new address entry. If the AddressID is NOT empty, just associate it.
<%: Html.TextBoxFor(model => model.ContactInfo.FirstName)%>
<%: Html.TextBoxFor(model => model.ContactInfo.LastName)%>
<%:Html.TextBoxFor(model => model.ContactInfo.Address.AddressID)%>
<%:Html.TextBoxFor(model => model.ContactInfo.Address.City)%>
<%:Html.TextBoxFor(model => model.ContactInfo.Address.Street)%>
How can this be done?
Here is when I save the Entity
public ActionResult Create(Contact ContactInfo){
try
{
ContactInfo.IsActive = true;
_db.AddToContacts(ContactInfo);
_db.SaveChanges();
}
}