ASP.NET MVC绑定特定模型导致POST请求出错。

4

您好,我在我的控制器中定义了以下两个动作:

    [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

我尝试了几种方法,比如重命名参数和删除可编辑字段,但似乎模型类型是问题所在,可能有什么问题?
更新:如果我使用bind属性添加前缀,错误会消失,但我不确定为什么。此外,我的代码将无法正常工作,因为输入元素不提供前缀。

是的。但它实现了一些接口,比如IPrincipal。 - TomHastjarjanto
2个回答

5

谷歌上有几个相同的问题,但都没有得到解答。我建议检查是否存在重复名称的属性:例如,使用“new”覆盖属性,或者在不同的接口中使用相同的名称等。

此外,我认为您可以参考ASP.NET MVC源代码,了解DefaultModelBinder中确切发生了什么。


可能有一些属性属于同一个接口,这会是个问题吗? - TomHastjarjanto
我重构了一个接口,发现似乎有一个重复的字段,现在它可以工作了,谢谢。 - TomHastjarjanto

0

我在MVC 2中遇到了同样的问题,即将数据发送回控制器。问题最终与EntityFramework和BindAttribute(用于验证)有关。

在我的情况下,我有一个名为Student的表格,其中包含一个名为nationality的字段。我正在重构一个旧数据库,并添加了一个新的Nationality表格,其中包含一个指向Student的链接以及一个新的NationalityID字段作为外键。

问题是,我没有重命名原始的nationality字段。现在,Student实体具有与字段(nationality)相同名称的关联(与Nationality表格)。

此外,我一直在使用BindAttribute进行验证,并将nationality字段标记为必填项。

我将数据库和实体框架中的nationality字段重命名为nationality_old。这消除了错误。希望这能帮助到某些人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接