I’ll explain what I’m trying to do real quick.
I have a page where I can edit Clients. A Client also has a collection of ClientContacts, related through a foreign key in the database. When editing a Client, you can also add/edit all the ClientContacts on the same page.
Here’s my view models:
public class ClientViewModel
{
[ScaffoldColumn(false)]
public int ClientId { get; set; }
[DisplayName("Name")]
public string Name { get; set; }
[DisplayName("Contacts")]
public List<ClientContactViewModel> ClientContacts { get; set; }
}
public class ClientContactViewModel
{
[ScaffoldColumn(false)]
public int ClientContactId { get; set; }
[DisplayName("Client")]
public int ClientId { get; set; }
[Required(ErrorMessage = "Name is required")]
[StringLength(100, ErrorMessage = "Name must be 100 characters or less")]
[DisplayName("Name")]
public string Name { get; set; }
}
And my controller method to edit a Client:
[HttpPost]
public ActionResult Edit(ClientViewModel viewModel)
{
// get client
Client client = _clientsRepository.GetClient(viewModel.ClientId);
client.Name = viewModel.Name;
if (ModelState.IsValid)
{
// save client contacts
if (viewModel.ClientContacts != null)
foreach (var clientContact in viewModel.ClientContacts)
client.ClientContacts.Add(new ClientContact
{
ClientID = client.ClientID,
Name = clientContact.Name
});
_clientsRepository.SaveClient(client);
return RedirectToAction("Index");
}
return View(viewModel); // validation error, so redisplay same view
}
My problem is this: Say this Client that I’m editing already has a ClientContact – ID: 1, Name: Client1. If I edit this Client, add another ClientContact and then save my Client, it doesn’t edit the existing ClientContact, it adds it along with the new ClientContact, so I get:
ID: 1, Name: Client1
ID: 2, Name: Client1
ID: 3, Name: Client2
So instead of having 2 ClientContacts in the database, the Client has 3, with a duplicate of the existing ClientContact
Note that I can’t clear out all the ClientContacts in the database for the Client first, as there is a bunch of data in other tables that related to each ClientContact.
How can I change my Edit method to fix this?
EDIT: I should also mention that the ClientContactID is passed to viewModel.ClientContacts for existing ClientContacts, and the ClientContactID is 0 if it’s a new contact. I imagine I can do a check to see if the ClientContactID is 0 on each iteration, but I’m not sure where to go with it after that
okay so I hope I understand this correctly. You are adding all of the client contacts to the ClientContacts List on an edit and then calling SaveClient() so your code above is creating the duplicate issue.
here is the problem essentially:
Because it always adds every clientcontact from the viewmodel to the client’s clientcontact collection.
If a client contact already exists in the db it most likely has a clientcontactid property that you can check and it will be greater than 0.
something like
is something like that what you are looking for?