MVC2中出现一个相同的键已被添加的问题原因

来源:互联网 发布:淘宝花费总额查询 编辑:程序博客网 时间:2024/05/16 10:24

 这两天用MVC2搞了一个软件,结果今天调试时碰到一个很头疼的问题。

只要使用HtmlHelper的控件模版在界面上显示控件,如下:

就报出An item with the same key has already been added这个错误。

后来在国外网站上才查到一个老兄也碰到这个问题,原因是对应的实体中有重名的属性。

像我这里 UserInfo实体类中有一个"ID"属性,这个实体类继承自EntityBase类,基类中有一个属性"Id".

注意这里的两个属性大小写不同,C#会认为是两个属性,但是在MVC的Request.Form中Key值是不区分大小写的。

这样就会出现添加重复键值的错误。

这个错误很少发生,因为如果大小写相同它也会被认为是一个属性,只有大小写不同时才会发生。

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) +52   System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +9383019   System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) +252   System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer) +91   System.Web.Mvc.ModelBindingContext.get_PropertyMetadata() +228   System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +392   System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +147   System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +98   System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +2503   System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +548   System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +474   System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +181   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830   System.Web.Mvc.Controller.ExecuteCore() +136   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +111   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +65   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +42   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +141   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +52   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8836977   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

原创粉丝点击