簡體   English   中英

asp.net MVC綁定特定模型導致發布請求的錯誤

[英]asp.net MVC binding specific model results in error for post request

嗨,我在控制器中定義了以下兩個操作

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

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


        return View();
    }

但是,如果我將編輯后的數據發布到第二個操作,我會收到以下錯誤:

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

我嘗試了幾個重命名參數和刪除可編輯字段的東西,但似乎模型類型是問題,可能是什么錯誤?

更新如果我使用綁定屬性添加前綴錯誤消失,但我不知道為什么..因此我的代碼將無法工作,因為輸入元素不提供前綴

谷歌上有幾個相同的問題,但沒有一個回答。 我建議檢查是否存在具有重復名稱的屬性:例如,對屬性使用“new”覆蓋,或者在不同的接口中使用相同的名稱等。

另外我認為您可以參考ASP.NET MVC源來查看DefaultModelBinder中到底發生了什么。

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

在我的情況下,我有一張名為學生的桌子,有外地國籍。 我正在重構一個舊數據庫,並添加了一個新表,其中包含指向Student的鏈接,以及一個新的INTERNATIONALID字段作為外鍵。

問題是,我沒有重命名原來的國籍領域。 Student實體現在具有與字段(國籍)同名的關聯(具有國籍表)。

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

我在國際數據庫和實體框架中將國籍字段重命名為nationality_old。 這擺脫了錯誤。 希望這有助於某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM