Upgrading to net core 3.1 – removal of [System.Web.Http.FromBody] breaks ajax response

Solution for Upgrading to net core 3.1 – removal of [System.Web.Http.FromBody] breaks ajax response
is Given Below:

[System.Web.Http.FromBody] doesn’t seem to exist in net core, and it seems like it was instrumental in making this ajax response work in asp net mvc.

Here’s the Ajax:

    $(document).on('click', '#txtBanishedPersonAlias', function (e) {

        e.preventDefault();

        var token = $('input[name=__RequestVerificationToken]').val();

        var varModel = {};
        varModel.BanishedAlias = $("#txtAliasEntry").val();
        varModel.BanishedPersonId = $("#BanishedPerson_Id").val();


        $.ajax({
            url: serverRoot + "/Webforms/SaveBanishedPersonAlias",
            method: "POST",
            responseType: 'json',
            data: {
                __RequestVerificationToken: token,
                model: varModel,

            },
            success: function (data) {

                for (var i = 0; i < data.length; i++) {

                    $("#InsertAliasList").append("<p class="tempFileListItem" style="color:black">" + data[i].BanishedAlias + "</p>");

                };

                console.log(JSON.stringify(data));
                //alert(JSON.stringify(data));
            },
            error: function () {
                alert("There has been an error.  Please grab a screenshot and contact the LIBC Help Desk.");
            }
        })
    });

Here’s the Controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    [Authorize(Roles = "Admin, AADUser")]
    public async Task<ActionResult> SaveBanishedPersonAliasAsync([System.Web.Http.FromBody] BanishedPersonAlias model)
    {

        // Gets Logged in User's info from the Registered user.
        var user = await _userManager.GetUserAsync(User);

        model.SubmitterId = user.Id;


        if (ModelState.IsValid)
        {

            if (model.Id == 0)
            {
                _dbcontext.BanishedPersonAlias.Add(model);
            }

            _dbcontext.SaveChanges();

        }

        // linq example for returning a single column of children.
        // Remember that error occurs if selecting the virtual property of the Parent Reference.
        var queryAliases =
            from bpa in _dbcontext.BanishedPersonAlias
            where bpa.BanishedPersonId == model.BanishedPersonId
            orderby bpa.BanishedAlias ascending
            //select bpa.BanishedAlias;

            // Example of selecting multiple columns to pass back to the view.
            select new { bpa.BanishedAlias, bpa.BanishedPersonId };

        return Json(queryAliases);

    }

Without the [System.Web.Http.FromBody], the controller correctly enters the new entry into the db, but the response on the page shows as “undefined.”

On inspection of the Response, though, i see all of the proper results – [{“banishedAlias”:”f”,”banishedPersonId”:1006},{“banishedAlias”:”fff”,”banishedPersonId”:1006},{“banishedAlias”:”fffe”,”banishedPersonId”:1006},{“banishedAlias”:”testalias1″,”banishedPersonId”:1006},{“banishedAlias”:”testalias2″,”banishedPersonId”:1006}]

but the response on the page shows as “undefined.”
On inspection of the Response, though, i see all of the proper results

You need change data[i].BanishedAlias to data[i].banishedAlias:

$("#InsertAliasList").append("<p class="tempFileListItem" style="color:black">" + data[i].banishedAlias + "</p>");

[System.Web.Http.FromBody] doesn’t seem to exist in net core

FromBody exsits in Microsoft.AspNetCore.Mvc namespace in asp.net core. System.Web.Http.FromBody is in asp.net.

Reference: FromBodyAttribute Class