2010-04-27 44 views
3

嗨,我有我的控制器asp.net MVC結合錯誤的具體型號結果POST請求

[Authorize] 
    [HttpGet] 
    public ActionResult Edit() 
    { 
     ViewData.Model = HttpContext.User.Identity; 
     return View(); 
    } 

    [Authorize] 
    [HttpPost] 
    public ActionResult Edit(User model) 
    { 


     return View(); 
    } 

但是定義了以下兩個動作,如果我後我editted數據,第二個動作我得到的以下錯誤:

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) +51 
    System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +7464444 
    System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) +270 
    System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer) +102 
    System.Web.Mvc.ModelBindingContext.get_PropertyMetadata() +157 
    System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +158 
    System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +90 
    System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +50 
    System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1048 
    System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +280 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +257 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +109 
    System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +314 
    System.Web.Mvc.Controller.ExecuteCore() +105 
    System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39 
    System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7 
    System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34 
    System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21 
    System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +59 
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44 
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +7 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8679150 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 

我試過幾件事情像重命名參數和刪除可編輯的字段,但似乎型號類型的問題,這可能是錯了嗎?

更新,如果我使用綁定添加前綴屬性錯誤消失,但我不知道我的why..also代碼就無法正常工作,輸入要素不提供前綴

+0

用戶是您定義的類型嗎? – 2010-04-27 20:13:59

+0

是的。但它實現了一些接口,如IPrincipal – TomHastjarjanto 2010-04-27 20:32:38

回答

5

谷歌有幾個相同的問題,但沒有人回答。我建議檢查是否有名稱重複的屬性:例如,使用「新」覆蓋屬性,或相同的名稱,但在不同的界面等。

另外我想你可以參考ASP.NET MVC資源來查看在DefaultModelBinder中究竟發生了什麼。

+0

可能有一些屬性是相同接口的一部分,這是否會成爲問題? – TomHastjarjanto 2010-04-27 20:51:38

+0

我重構了一個接口,它似乎有一個重複的字段,現在它的工作謝謝 – TomHastjarjanto 2010-04-27 21:02:56

0

我有同樣的問題,回到MVC 2中的控制器。這個問題最終與EntityFramework和BindAttribute(用於驗證)相關。

就我而言,我有一張名爲學生的表格,具有字段國籍。我重構了一箇舊的數據庫,並添加了一個新的表格國籍和一個新的國籍ID字段作爲外鍵。

問題是,我沒有重命名原來的國籍字段。學生實體現在有一個與國籍(國籍)同名的關聯(國籍表)。

此外,我一直在使用BindAttribute進行驗證,並將國籍字段標記爲必需。

我在數據庫和實體框架中將國籍字段更名爲nationality_old。這擺脫了錯誤。希望這可以幫助某人。