2011-03-10 114 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

相關問題