Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5 - An item with the same key has already been added

Tags:

asp.net-mvc

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.

like image 367
Randy Minder Avatar asked Feb 10 '23 21:02

Randy Minder


1 Answers

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.

like image 115
Ravi Teja Koneru Avatar answered Feb 14 '23 01:02

Ravi Teja Koneru