简体   繁体   中英

ASP.NET Core http post [FromBody] broken in 3.1, used to work in 2.2

I am having all kinds of errors migrating from ASP.NET Core 2.2, to 3.1.

My latest error is that the object I am receiving via a Http Post is null.

Here is the code that receives the object, and in this case the model is null.

[HttpPost]
public async Task<IActionResult> MyAction([FromBody] BoardMoveModel model)

The model class:

public class BoardMoveModel
{
    public int BoardId { get; set; }
    public int TicketId { get; set; }
    public int DestinationStatusId { get; set; }
    public int SourceStatusId { get; set; }
    public IEnumerable<int> SourceStatusList {get; set;}
    public IEnumerable<int> DestinationStatusList {get; set;}
}

And the Javascript code:

        var data = {
            TicketId: el.id,
            DestinationStatusId: targetStatusId,
            SourceStatusId: sourceStatusId,
            SourceStatusList: sourceList,
            DestinationStatusList: destList
        };

        $.ajax({
            async: true,
            url: "/Area/Board/MyAction",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            headers: {
                RequestVerificationToken:
                $('input:hidden[name="__RequestVerificationToken"]').val()
            },
            data: JSON.stringify(data)
        }).done(function(result) {

Here is the actual JSON Payload:

             {
                 "TicketId":"150",
                 "DestinationStatusId":"5",
                 "SourceStatusId":"6",
                 "SourceStatusList":[],
                 "DestinationStatusList":["140","150"]
             }

Model is null because it's invalid for binding. In JSON you are passing in string values while model requires integers. Change either to match and it should work. Binding is by default case sensitive, but it shouldn't be a problem with code you've provided.

Try sending this as application/json body

{
    "TicketId":150,
    "DestinationStatusId":5,
    "SourceStatusId":6,
    "SourceStatusList":[],
    "DestinationStatusList":[140,150]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM