开发者

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

Hi I'm having the following two actions defined in my controller

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

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


        return View();
    }

Howev开发者_开发百科er if I post my editted data to the second action I get the following error:

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

I tried several things like renaming parameters and removing editable fields, but it seems the model type is the problem, what could be wrong?

Update if I add a prefix using the bind attribute the error goes away, but I'm not sure why..also my code will then not work as the input elements don't provide a prefix


There're several same issues on google but none of them answered. I'd suggest to check if there're properties with duplicated names: for example, using "new" override for property, or same names but in different interfaces, etc.

Also I think you can refer to ASP.NET MVC sources to see what exactly happens in DefaultModelBinder.


I had the same problem, posting back to the controller in MVC 2. The problem ended up being related to the EntityFramework and the BindAttribute (used for validation).

In my case, I had a table called Student with a field nationality. I'm refactoring an old database, and added a new table Nationality with a link to Student, and a new NationalityID field as foreign key.

The problem was, I didn't rename the original nationality field. The Student entity now had an association (with the Nationality table) with the same name as a field (nationality).

In addition, I had been using the BindAttribute for validation, and had marked the nationality field as Required.

I renamed the nationality field to nationality_old in the database and in entity framework. This got rid of the error. Hope this helps someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜