升级到 net core 3.1 - 删除 [System.Web.Http.FromBody] 会破坏 ajax 响应

时间:2021-08-02 00:39:54

标签: json ajax asp.net-core model-view-controller controller

[System.Web.Http.FromBody] 似乎不存在于 net core 中,它似乎有助于使这个 ajax 响应在 asp net mvc 中工作。

这是阿贾克斯:

    $(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.");
            }
        })
    });

这是控制器:

    [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);

    }

如果没有 [System.Web.Http.FromBody],控制器正确地将新条目输入到数据库中,但页面上的响应显示为“未定义”。

在检查响应时,我看到了所有正确的结果 - [{"banishedAlias":"f","banishedPersonId":1006},{"banishedAlias":"fff","banishedPersonId":1006 },{"banishedAlias":"fffe","banishedPersonId":1006},{"banishedAlias":"testalias1","banishedPersonId":1006},{"banishedAlias":"testalias2","banishedPersonId":1006}]

1 个答案:

答案 0 :(得分:1)

<块引用>

但页面上的响应显示为“未定义”。 但是,在检查响应时,我看到了所有正确的结果

您需要将 data[i].BanishedAlias 更改为 data[i].banishedAlias

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

[System.Web.Http.FromBody] 在网络核心中似乎不存在

FromBody 存在于 asp.net 核心的 Microsoft.AspNetCore.Mvc 命名空间中。 System.Web.Http.FromBody 在 asp.net 中。

参考:FromBodyAttribute Class