0

Deserialization of a dynamic object that works fine in .NET Core 2.2, but does not work in .NET Core 3.1.

I'm using Newtonsoft.Json nuget package version v12.0.3. It's an ASP.NET Core MVC project.

public IActionResult Apply([FromBody]dynamic postData)
{
    string serverPath = _hostingEnvironment.WebRootPath;

    List<ModelData> list = new List<ModelData>();

    foreach (var item in JsonConvert.DeserializeObject(postData))
    {
        list.Add(new ModelData
            {
                Key = item.Key,
                Value = item.Value
            });
    }
}

I need help to fix it

Input data:

ValueKind = String : "[{'Key':'Table','Value':'admins'},{'Key':'MethodValue','Value':'Update'},{'Key':'Page','Value':'1'},{'Key':'Id','Value':'1'},{'Key':'ImgPath','Value':''},{'Key':'Id','Value':'1'},{'Key':'Login','Value':'admin'},{'Key':'Password','Value':'12345678999'}]"

It's what I see using debugger

ModelData output

public struct ModelData
        {
            public string Key { get; set; }

            public string Value { get; set; }
        }

Code from client page

function SendFormData(fileName) {
        var jsonArr = "[{'Key':'Table','Value':'@ViewBag.Table'},{'Key':'MethodValue','Value':'@ViewBag.MethodValue'}," +
            "{'Key':'Page','Value':'@ViewBag.Page'},{'Key':'Id','Value':'@ViewBag.Id'},";

        jsonArr += "{'Key':'ImgPath','Value':'" + fileName + "'},";

        var other_data = $("#ApplyForm").serializeArray();
        $.each(other_data, function (_key, input) {
            if (input.name === "Status") {
                return true;
            }
            jsonArr += "{'Key':'" + input.name + "','Value':'" + input.value + "'},";
        });

        jsonArr = jsonArr.slice(0, -1);
        jsonArr += "]";

        $.ajax({
            url: "/Admin/Apply",
            type: "POST",
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(jsonArr),
            async: false,
            success: function (returnUrl) {
                AjaxLoad(returnUrl);
            },
            error: function () {
                console.log("@Translations.UpsErrorMessage");
            }
        });
    }
Kanasi
  • 59
  • 2
  • 12
  • What is the input? What is the expected output? What is the actual output? – omajid Mar 06 '20 at 20:08
  • @OmairMajid ValueKind = String : "[{'Key':'Table','Value':'admins'},{'Key':'MethodValue','Value':'Update'},{'Key':'Page','Value':'1'},{'Key':'Id','Value':'1'},{'Key':'ImgPath','Value':''},{'Key':'Id','Value':'1'},{'Key':'Login','Value':'admin'},{'Key':'Password','Value':'12345678999'}]" – Kanasi Mar 06 '20 at 20:08
  • please also add the `ModelData` class – Pac0 Mar 06 '20 at 20:11
  • 1
    the string you have is not a JSON. JSON use double quotes `"` for field name and string values, not single quote `'` – Pac0 Mar 06 '20 at 20:12
  • @Pac0 Done it. I added. But in version 2.2 works fine. Version 3.1 don't want to deserialize. Wrote me that I have invalid arguments. – Kanasi Mar 06 '20 at 20:18
  • 1
    Did you follow the instructions from [Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?](https://stackoverflow.com/a/55666898/3744182) to switch your app back to Json.NET from [tag:system.text.json]? – dbc Mar 06 '20 at 20:29
  • @Pac0 Yes, thanks!!!!! It's an answer! Need just added right nuget package and add config line to sturtup file. P.S. ' quote or " quote it doesn't matter, works fine with all :) – Kanasi Mar 06 '20 at 20:38
  • That's not me who gave the last comment, it's @dbc ;) – Pac0 Mar 06 '20 at 20:51
  • @Zicise - glad that helped. OK to mark this question as a duplicate then? – dbc Mar 06 '20 at 20:57

1 Answers1

0

That seems client fail to send valid postData (no unique), and that is no need to stringify any string variable, except it is an array as following:

function SendFormData(fileName) {
    var jsonArr = [{'Key':'Table',''Value':'@ViewBag.Table'},
       {'Key':'MethodValue','Value':'@ViewBag.MethodValue'},
       {'Key':'Page','Value':'@ViewBag.Page'},
       {'Key':'ImgPath','Value':fileName}],
         other_data = $("#ApplyForm").serializeArray();

    $.each(other_data, function (_key, input) {
        if (input.name === "Status") {
            return true;
        }
        jsonArr.push({Key: input.name, Value: input.value});
    });

    $.ajax({
        url: "/Admin/Apply",
        type: "POST",
        contentType: 'application/json; charset=utf-8',
        data: {postData:JSON.stringify(jsonArr)},
        async: false,
        success: function (returnUrl) {
            AjaxLoad(returnUrl);
        },
        error: function () {
            console.log("@Translations.UpsErrorMessage");
        }
    });
}
OO7
  • 660
  • 4
  • 10