Results 1 to 5 of 5

Thread: an item with the same key has already been added.

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2019
    Posts
    44

    an item with the same key has already been added.

    in mvc how can i solve an item with the same key has already been added issue.
    < advertising removed by moderator >

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: an item with the same key has already been added.

    You're going to have to provide much more information than that. You need to describe exactly what circumstances under which you got that error message. You need to provide a FULL and CLEAR explanation of the problem.

  3. #3
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: an item with the same key has already been added.

    To add what jmc said, you need to not add the same key.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: an item with the same key has already been added.

    I can only speculate as to what is causing this issue, but I suspect that you have declared a Dictionary(Of TKey, TValue) and you're attempting to add multiple records, but one of these records share the same key.

    If you're taking in a JSON request then attempt to deserialize to a List(Of T) where the type is a KeyValuePair. This is so you can setup a break point, get all the Keys from the KeyValuePairs, and find the duplicate in the intermediate winow.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: an item with the same key has already been added.

    Quote Originally Posted by kfcSmitty View Post
    To add what jmc said, you need to not add the same key.
    Quote Originally Posted by dday9 View Post
    I can only speculate as to what is causing this issue, but I suspect that you have declared a Dictionary(Of TKey, TValue) and you're attempting to add multiple records, but one of these records share the same key.

    If you're taking in a JSON request then attempt to deserialize to a List(Of T) where the type is a KeyValuePair. This is so you can setup a break point, get all the Keys from the KeyValuePairs, and find the duplicate in the intermediate winow.
    I don't think that this is a case of explicitly adding an item to your own Dictionary. I've seen this type of error before in MVC but I can't recall the specific cause or whether it's always the same. The Dictionary in question is something within MVC and I think that it might have something to do with model binding; perhaps two fields in a view trying to generate field son the model with the same name or something like that. Of course, the same rules apply as when debugging anything, i.e. just stating the error message with no context is rarely enough.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width