C# 错误:已添加具有相同密钥的项。问题在哪里?
在发帖方法上我得到了一个错误,无法理解,无法找到原因。我想,也许,在我们的视野中,会有一些错误,但在哪里却看不到 整个项目是 //模型C# 错误:已添加具有相同密钥的项。问题在哪里?,c#,asp.net,.net,asp.net-mvc,asp.net-mvc-5,C#,Asp.net,.net,Asp.net Mvc,Asp.net Mvc 5,在发帖方法上我得到了一个错误,无法理解,无法找到原因。我想,也许,在我们的视野中,会有一些错误,但在哪里却看不到 整个项目是 //模型 public class IndexViewModel { public bool HasPassword { get; set; } public IList<UserLoginInfo> Logins { get; set; } public string PhoneNumber { get; set; } pub
public class IndexViewModel
{
public bool HasPassword { get; set; }
public IList<UserLoginInfo> Logins { get; set; }
public string PhoneNumber { get; set; }
public bool TwoFactor { get; set; }
public bool BrowserRemembered { get; set; }
public ICollection<MembershipTypes> MembershipTypes { get; set; }
[Required]
public int membershipTypeId { get; set; }
[Display(Name = "First Name")]
public string fname { get; set; }
[Display(Name = "First Name")]
public string lname { get; set; }
[Display(Name = "Phone")]
public string phone { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Birth Date")]
[DisplayFormat(DataFormatString = "{0:MMM dd yyyy}")]
public DateTime bdate { get; set; }
[Required]
[Display(Name = "Membership Type")]
public string membershipTypeID { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email")]
public string mail { get; set; }
}
公共类索引模型
{
公共bool HasPassword{get;set;}
公共IList登录名{get;set;}
公共字符串PhoneNumber{get;set;}
公共布尔双因子{get;set;}
公共布尔浏览器{get;set;}
公共ICollection成员类型{get;set;}
[必需]
public int membershipTypeId{get;set;}
[显示(Name=“First Name”)]
公共字符串fname{get;set;}
[显示(Name=“First Name”)]
公共字符串lname{get;set;}
[显示(Name=“Phone”)]
公用字符串电话{get;set;}
[数据类型(DataType.Date)]
[显示(Name=“出生日期”)]
[DisplayFormat(DataFormatString=“{0:MMM dd yyyy}”)]
公共日期时间bdate{get;set;}
[必需]
[显示(Name=“会员类型”)]
公共字符串membershipTypeID{get;set;}
[必需]
[数据类型(数据类型.电子邮件地址)]
[显示(Name=“电子邮件”)]
公共字符串邮件{get;set;}
}
//这是我得到的一个错误
Server Error in '/' Application.
An item with the same key has already been added.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: An item with the same key has already been added.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentException: An item with the same key has already been added.]
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +56
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +12948192
System.Collections.Generic.CollectionExtensions.ToDictionaryFast(TValue[] array, Func`2 keySelector, IEqualityComparer`1 comparer) +116
System.Web.Mvc.ModelBindingContext.get_PropertyMetadata() +149
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +176
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +101
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +55
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1209
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +333
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +343
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105
System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__0(AsyncCallback asyncCallback, Object asyncState) +640
System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +346
System.Web.Mvc.<>c.<BeginExecuteCore>b__152_0(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +27
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +494
System.Web.Mvc.<>c.<BeginExecute>b__151_1(AsyncCallback asyncCallback, Object callbackState, Controller controller) +16
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +403
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
System.Web.Mvc.<>c.<BeginProcessRequest>b__20_0(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +54
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +427
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +105
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4075.0
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
“/”应用程序中出现服务器错误。
已添加具有相同密钥的项。
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。
异常详细信息:System.ArgumentException:已添加具有相同键的项。
源错误:
在执行当前web请求期间生成了未经处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别。
堆栈跟踪:
[ArgumentException:已添加具有相同密钥的项。]
系统.ThrowHelper.ThrowArgumentException(异常资源)+56
System.Collections.Generic.Dictionary`2.Insert(TKey、TValue、布尔加法)+12948192
System.Collections.Generic.CollectionExtensions.ToDictionaryFast(TValue[]数组,Func`2键选择器,IEqualityComparer`1比较器)+116
System.Web.Mvc.ModelBindingContext.get_PropertyMetadata()+149
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext ControllerContext,ModelBindingContext bindingContext,PropertyDescriptor PropertyDescriptor)+176
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext ControllerContext,ModelBindingContext bindingContext)+101
System.Web.Mvc.DefaultModelBinder.BindCompleteXelementalModel(ControllerContext ControllerContext,ModelBindingContext bindingContext,Object model)+55
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)+1209
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)+333
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext ControllerContext,ParameterDescriptor ParameterDescriptor)+343
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor)+105
System.Web.Mvc.Async.c__显示类3_1.b__0(AsyncCallback AsyncCallback,对象asyncState)+640
System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback回调,对象callbackState)+14
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(异步回调,对象状态,Int32超时)+128
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext ControllerContext,String actionName,AsyncCallback回调,对象状态)+346
System.Web.Mvc.c.b__152_0(AsyncCallback AsyncCallback,Object asyncState,ExecuteCoregreat innerState)+27
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback回调,对象callbackState)+30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(异步回调,对象状态,Int32超时)+128
System.Web.Mvc.Controller.BeginExecuteCore(异步回调,对象状态)+494
System.Web.Mvc.c.b__151_1(AsyncCallback AsyncCallback、Object callbackState、Controller)+16
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback回调,对象callbackState)+20
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(异步回调,对象状态,Int32超时)+128
System.Web.Mvc.Controller.BeginExecute(RequestContext RequestContext,AsyncCallback回调,对象状态)+403
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext RequestContext,AsyncCallback回调,对象状态)+16
System.Web.Mvc.c.b__20_0(AsyncCallback AsyncCallback,Object asyncState,ProcessRequestState innerState)+54
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback回调,对象callbackState)+30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(异步回调,对象状态,Int32超时)+128
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,异步回调,对象状态)+427
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext HttpContext,异步回调,对象状态)+48
System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.BeginProcessRequest(HttpContext上下文,AsyncCallback cb,Object extraData)+16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+105
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep步骤)+50
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+163
版本信息:Microsoft.NET Framework版本:4.0.30319;ASP.NET版本:4.8.4075.0
----------
----------
----------
----------
----------
----------
----
public class IndexViewModel
{
public bool HasPassword { get; set; }
public IList<UserLoginInfo> Logins { get; set; }
public string PhoneNumber { get; set; }
public bool TwoFactor { get; set; }
public bool BrowserRemembered { get; set; }
public ICollection<MembershipTypes> MembershipTypes { get; set; }
[Required]
public int membershipTypeId { get; set; }
[Display(Name = "First Name")]
public string fname { get; set; }
[Display(Name = "First Name")]
public string lname { get; set; }
[Display(Name = "Phone")]
public string phone { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Birth Date")]
[DisplayFormat(DataFormatString = "{0:MMM dd yyyy}")]
public DateTime bdate { get; set; }
[Required]
[Display(Name = "Membership Type")]
public string membershipTypeID { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email")]
public string mail { get; set; }
}
Server Error in '/' Application.
An item with the same key has already been added.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: An item with the same key has already been added.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentException: An item with the same key has already been added.]
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +56
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +12948192
System.Collections.Generic.CollectionExtensions.ToDictionaryFast(TValue[] array, Func`2 keySelector, IEqualityComparer`1 comparer) +116
System.Web.Mvc.ModelBindingContext.get_PropertyMetadata() +149
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +176
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +101
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +55
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1209
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +333
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +343
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105
System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__0(AsyncCallback asyncCallback, Object asyncState) +640
System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +346
System.Web.Mvc.<>c.<BeginExecuteCore>b__152_0(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +27
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +494
System.Web.Mvc.<>c.<BeginExecute>b__151_1(AsyncCallback asyncCallback, Object callbackState, Controller controller) +16
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +403
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
System.Web.Mvc.<>c.<BeginProcessRequest>b__20_0(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +54
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +427
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +105
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4075.0
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
[Required]
public int membershipTypeId { get; set; }
...
[Required]
[Display(Name = "Membership Type")]
public string membershipTypeID { get; set; }
@Html.HiddenFor(m => m.membershipTypeId)
...
@Html.DropDownListFor(m => m.membershipTypeId, new SelectList(Model.MembershipTypes, "membershipTypesIdPK", "name"), new { @class = "form-control" })
@Html.HiddenFor(m => m.membershipTypeId)