Alex Zhukovskiy
Alex Zhukovskiy

Reputation: 10025

ASP.Net MVC doesn't map parameters for ajax call

I want to send a request with several parameters in ASP.Net Core 1.0 (I don't know if it matters).

I have following model:

[DataContract]
public class RegistrationConfigContract
{
    [DataMember]
    public string ServiceType { get; set; }
    [DataMember]
    public int BatchTimeout { get; set; }
    [DataMember]
    public int BatchSize { get; set; }
    [DataMember]
    public ActorFinishingTypeEnum FinishingType { get; set; }
}

I have following controller method:

public void SendRegistration(string nodeName, NodeTypeEnum nodeType, RegistrationConfigContract model)
{

}

I also have a JS that tries to call it:

function sendRegistration(nodeName, nodeType) {
    var model = {
        ServiceType: $("#serviceType").html(),
        BatchTimeout: $("#batchTimeout").html(),
        BatchSize: $("#batchSize").html(),
        FinishingType: $("#finishingType").html()
    };

    var request = {
        nodeName: nodeName,
        nodeType: nodeType,
        model: model
    }

    //$.post('@Url.Action("SendRegistration")', request);
    $.ajax({
        url: '@Url.Action("SendRegistration")',
        type: "POST",
        dataType: 'json',
        data: JSON.stringify(request),
        async: false,
        cache: false,
        traditional: true,
        contentType: 'application/json'
    });
}

But on server side I always get nulls while on JS side everything is fine, according to Chrome/Edge debugger.

What am I doing wrong here? I tried to use code I googled for this problem, but it doesn't work.

Raw HTTP request:

POST http://localhost:8080/Configuration/SendRegistration HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
Accept: application/json, text/javascript; q=0.01
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8080/Configuration
Content-Length: 156
X-Compress: 1
Proxy-Authorization: 6e9b34bd44817cf5c254e1ccb4fe7b31ecd526ea9e025e06a16baca626af6be0ea7fa102c2205f0e
Connection: keep-alive

{"nodeName":"zhdp","nodeType":"DocumentProducer","model":{"ServiceType":"jjjjjj","BatchTimeout":"33535","BatchSize":"6666","FinishingType":"UpdateMessage"}}

Upvotes: 1

Views: 2034

Answers (2)

Alex Zhukovskiy
Alex Zhukovskiy

Reputation: 10025

It seems that it's an ASP.Net Core bug (or feature). I mixed both query string parameters and model. After I did it, it can bind query string but cannot bind request body. After adding [FromBody] attribute everything work well.

JS:

function sendRegistration(nodeName, nodeType) {
    var model = {
        ServiceType: $("#serviceType").html(),
        BatchTimeout: $("#batchTimeout").html(),
        BatchSize: $("#batchSize").html(),
        FinishingType: $("#finishingType").html()
    };

    $.ajax({
        url: '@Url.Action("SendRegistration")?nodeName={0}&nodeType={1}'.f(nodeName, nodeType),
        type: "POST",
        dataType: 'json',
        data: JSON.stringify(model),
        cache: false,
        traditional: true,
        contentType: 'application/json'
    });
}

where f is string.Format analogue. And C# side:

[HttpPost]
public void SendRegistration(string nodeName, NodeTypeEnum nodeType, [FromBody] RegistrationConfigContract model)
{

}

[HttpPost] is not necessary here, but it show my intention better than its absence.

Upvotes: 0

enet
enet

Reputation: 45656

The problem is with model binding... Model Binding in ASP.NET Core is different. You should mark your action's parameters with appropriate attributes such as [FromBody] or [FromForm]. Also, you can either have a normal form post or Ajax post, but not both...

More info here: https://andrewlock.net/model-binding-json-posts-in-asp-net-core/

Upvotes: 1

Related Questions