private static void SetContacts(Person person, IList<Contact> contacts, ModelStateDictionary modelState)
{
// remove the blanks
var remove = contacts.Where(a => !a.HasContact).ToList();
foreach (var a in remove) contacts.Remove(a);
// update/add contacts
foreach (var ct in contacts)
{
var type = ct.ContactType;
var origCt = person.Contacts.Where(a => a.ContactType == type).FirstOrDefault();
if (type.Required)
{
ct.Person = person;
ct.TransferValidationMessagesTo(modelState);
}
if (ct.HasContact)
{
if (origCt == null)
{
person.AddContact(ct);
}
else
{
Mapper.Map(ct, origCt);
}
}
else
{
if (origCt != null) person.Contacts.Remove(ct);
}
}
}