2011-03-10 112 views
0

我的控制器没有在提交时从视图页面调用,而是在我点击提交按钮时收到错误消息。On submit throws ArgumentException:已添加具有相同密钥的项目

错误:具有相同密钥的项目已被添加。

堆栈跟踪:

[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) +9382923 
    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) +2504 
    System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +548 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +473 
    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 
+1

代码在哪里? – Vadim 2011-03-10 04:25:06

+0

一个猜测 - 你有相同的ID在窗体中的两个元素。 7年后的 – RPM1984 2011-03-10 05:35:40

回答

0

可能需要一些更多的代码来回答你的问题,像Aciton方法和形式。

我的猜测是模型绑定试图添加具有相同名称/ ID的两个表单值并且在那里失败,所以我会检查您的视图代码以确保您没有使用相同的ID标记两次。

3

您的viewmode必须具有两个具有相同名称的属性,例如UserName和userName。当MVC进行模型绑定时,它不区分大小写,它会认为你有两个相同的属性。

+0

;这是有帮助的。 – mzonerz 2017-04-23 20:25:19

相关问题