Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:11:12+00:00 2026-05-29T21:11:12+00:00

I’ll explain what I’m trying to do real quick. I have a page where

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:11:14+00:00Added an answer on May 29, 2026 at 9:11 pm

    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:

    foreach (var clientContact in viewModel.ClientContacts)
                    client.ClientContacts.Add(new ClientContact
    

    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

    foreach(ClientContact clientContact in viewModel.ClientContacts)
    {
      if (clientContact.ClientContactId ==0)
          //add it to the collection here
    }
    

    is something like that what you are looking for?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.