asp.net mvc 3 upgrade to MVC 5.1: "An item with the same key has been added"

Source: Internet
Author: User
Tags json lowercase

A project was recently upgraded from ASP.net MVC 3 to just released ASP.net MVC 5.1, and an AJAX request was found to have 500 errors, with detailed exception information logged in the log as follows:

System.ArgumentException: An item with the same key has been added. (an item with the same key has already been added) in System.Collections.Generic.Dictionary ' 2.Insert (TKey key, TValue VA Lue, Boolean Add) in System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore (Entrylimiteddictionary Backingstore, String prefix, Object value) in System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore (entrylimiteddictionary Backingstore, String prefix, Object value) in System.Web.Mvc.JsonValueProviderFactory.GetValueProvider ( ControllerContext controllercontext) in System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider ( ControllerContext controllercontext) in System.Web.Mvc.ControllerBase.get_ValueProvider () at System.Web.Mvc.Controlle Ractioninvoker.getparametervalue (ControllerContext controllercontext, Parameterdescriptor ParameterDescriptor) in System.Web.Mvc.ControllerActionInvoker.GetParameterValues (ControllerContext ControllerContext, Actiondescriptor Actiondescriptor) in System.Web.Mvc.Async.AsyNccontrolleractioninvoker.<>c__displayclass21.<begininvokeaction>b__19 (AsyncCallback AsyncCallback, Object asyncstate) in System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase ' 1.Begin (AsyncCallback callback  , object state, Int32 timeout) in System.web.mvc.async.asyncresultwrapper.begin[tresult] (AsyncCallback callback, Object State, Begininvokedelegate begindelegate, endinvokedelegate ' 1 enddelegate, Object tag, Int32 timeout) in SYSTEM.WEB.MVC . Async.AsyncControllerActionInvoker.BeginInvokeAction (ControllerContext controllercontext, String ActionName, AsyncCallback callback, Object State)

Although the problem is due to the upgrade to MVC 5.1 caused, but the spirit of "encounter problems, first doubt yourself" principle, check the code, unexpectedly in the JS code found a long-standing low-level error:

var Pagingbuider = {"PageIndex": 1};
function buildpaging (pageIndex) {
    pagingbuider.pageindex = PageIndex;
    $.ajax ({
        data:JSON.stringify (pagingbuider),
        contentType: ' Application/json; Charset=utf-8 '
    });

PageIndex in the assignment of a pageindex (the 1th letter capital P written in lowercase p), in JS, the beginning of the letter lowercase is also the canonical writing, then may be intuitive to write out, so this low-level error understandable.

/* At this point you may not be tempted to ask: Why do you find yourself, the beginning of the letter in uppercase? Hey, I also have my difficulties, this JS code is generated in the server based on the properties of C # objects, C # is the specification of the initial letter capital * *

Because of such a low-level error, the JSON string sent to the server at the time of the AJAX request becomes this:

{"PageIndex": 1, "PageIndex": 2}

At this time, the vigor of picking up, a big question mark appeared in front of ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.