4

I realize there are many questions about this issue. But I have yet been able to resolve this, after spending many hours trying to track this down.

My model looks as follows:

public class Guide
    {
        public int GuideId { get; set; }
        public int GuideTypeId { get; set; }

        [Required(ErrorMessage = "Please enter a business name")]
        [StringLength(80, ErrorMessage="The Business Name cannot exceed 80 characters")]
        public string BusinessName { get; set; }

        public System.DateTime? ActiveStartDate { get; set; }
        public System.DateTime? ActiveEndDate { get; set; }

        [DataType(DataType.Url, ErrorMessage="The website url entered is not a valid url")]
        [StringLength(80, ErrorMessage = "The Website Url cannot exceed 80 characters")]
        public string WebsiteURL { get; set; }

        [Required(ErrorMessage = "Please enter a your primary location")]
        [StringLength(80, ErrorMessage = "The Primary Location cannot exceed 80 characters")]
        public string BaseLocation { get; set; }

        [DataType(DataType.EmailAddress, ErrorMessage = "The email address entered is not a valid email address")]
        [Required(ErrorMessage="Please enter a contact email address")]
        [StringLength(80, ErrorMessage = "The Email Address cannot exceed 80 characters")]
        public string EmailAddress { get; set; }

        [DataType(DataType.PhoneNumber, ErrorMessage = "The phone number entered is not a valid phone number")]
        [StringLength(20, ErrorMessage = "The Phone Number cannot exceed 20 characters")]
        public string PhoneNumber { get; set; }

        [Required(ErrorMessage = "Please enter a description of your fees")]
        public string FeesDescription { get; set; }

        public string VehiclesAvailable { get; set; }
        public string AdditionalInformation { get; set; }
        public string AccountName { get; set; }
        public string Countries { get; set; }
        public string NearestCities { get; set; }
        public string Locations { get; set; }
        public byte[] ProfileImage { get; set; }
        public bool Approved { get; set; }
        public System.DateTimeOffset LastModifiedTime { get; set; }

        // These four properties are here so we can display all tour types, durations, languages and currencies in a checkbox layout on the page. These are not
        // what the guide actually supports. These just represent all possible values.
        public List<TourType>     TourTypeList { get; set; }
        public List<TourDuration> TourDurationList { get; set; }
        public List<TourLanguage> TourLanguageList { get; set; }
        public List<TourCurrency> TourCurrencyList { get; set; }

        // These four properties represent what the guide does actually support
        public virtual List<GuideTourType>     GuideTourType { get; set; }
        public virtual List<GuideTourDuration> GuideTourDuration { get; set; }
        public virtual List<GuideTourLanguage> GuideTourLanguage { get; set; }
        public virtual List<GuideTourCurrency> GuideTourCurrency { get; set; }

        public bool HasImage
        {
            get
            {
                return ProfileImage.Length > 0;
            }
        }

        public virtual GuideType GuideType { get; set; }
    }

I did find someone who attempted to debug this using the following binding code:

public class DebugModelBinder : DefaultModelBinder, IModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            Dictionary<string, ModelMetadata> d = new Dictionary<string, ModelMetadata>(StringComparer.OrdinalIgnoreCase);
            foreach (var p in bindingContext.ModelMetadata.Properties)
            {
                var propertyName = p.PropertyName;
                try
                {
                    d.Add(propertyName, null);
                }
                catch (ArgumentException ex)
                {
                    throw new ArgumentException(String.Format("The Item {0} as already been added", propertyName), ex);
                }
            }
            return base.BindModel(controllerContext, bindingContext);
        }
    }

I was able to get this code to work, but the exception is occuring when this line of code is executed:

return base.BindModel(controllerContext, bindingContext);

So it's happening in the base class.

This exception is being generated whenI do a post on a form in an MVC 5 page. My C# controller method is never actually getting called. The error is occurring in the binding, and I have no idea what property is getting added twice. Nor do I know how to find out.

If you need more information, I'll be glad to provide it. I've wasted several hours on this.

Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • Does the action accept the `Guide` model as a parameter? You can try commenting out the properties one by one and find out which one is causing the problem. – Zabavsky Feb 22 '15 at 14:19
  • @zabavsky - that's exactly what I've started doing. I've removed most of them and my controller method is now getting called. Now I just need to find the offending property. – Randy Minder Feb 22 '15 at 14:24
  • What's the signature of your Controller Action method? – Chris Pietschmann Feb 23 '15 at 03:12
  • See http://stackoverflow.com/questions/5648060/an-item-with-the-same-key-has-already-been-added – Julian Dec 01 '15 at 09:10

4 Answers4

10

In my case, it occurred due duplicate attribute in a entity

public class Employee
{
public int roleID {get; set;}
...
...
public int RoleID {get; set;}
}

when I removed int Role , this error was gone.

Ravi Teja Koneru
  • 502
  • 7
  • 16
0

Check that your connection string of you web.config and the one in your new model are not the same...

So if you deleted your model and created it again, just temporarily change your name in the existing Connection String.

aesax
  • 35
  • 9
0

The solution is remove the duplicate property from your class or model

Muhammad Ayyaz
  • 11
  • 1
  • 14
0

I had the same error after attempting TryUpdateModel(myModel,"", new string[] { "Index", "Title", "aDate" })

I checked, double-checked and triple-checked the model controller and view for any duplications. Nada, None.

It had just worked the day before.

Finally, I exited Visual Studio, rebooted the machine and performed a Clean and Rebuild of the Solution. Now, the controller works exactly as intended.

Hope this saves someone a few hours :)

Mike
  • 419
  • 4
  • 6