c# - asp.net MVC 绑定(bind)特定模型导致发布请求出错

标签 c# asp.net-mvc

您好,我在我的 Controller 中定义了以下两个操作

    [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)属性添加前缀,错误就会消失,但我不确定为什么......我的代码也将无法工作,因为输入元素不提供前缀

最佳答案

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

此外,我认为您可以引用 ASP.NET MVC 源代码以查看 DefaultModelBinder 中到底发生了什么。

关于c# - asp.net MVC 绑定(bind)特定模型导致发布请求出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2724601/

相关文章:

c# - .Net 2.0、VS2010 和 Windows 8 上的 "Could not load file or assembly System.Drawing or one of its dependencies"错误

c# - 是否可以限制远程机器连接到 NamedPipeServerStream?

c# - ASP.NET 登录状态 : logout not working

c# - Windows 应用程序中的异常处理 C#

c# - 如何使我的自定义数据注释起作用(它在列表中,列表中的每个项目都会覆盖最后的验证测试)

C# - 如何将特定记录从数据库显示到 ASP.NET MVC 中的 View

c# - 在 ASP.Net/C# 中优化循环

c# - 内存流中的电子邮件附件出错,但仅在第一次发送后发生

jquery - 根据字段值动态生成超链接的查询字符串

jquery - 将对象列表发布到 MVC 5 Controller