1

新しい ASP.NET Web API でモデル バインドを機能させるのに問題があります。

私は jQuery を使用して投稿を作成しています。クライアント側の JavaScript は次のようになります。

function postJsonSettings() {
    return {
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        xhrFields: {
            withCredentials: true
        }
    };
}

function createCustomer(customer) {
    var settings = postJsonSettings();
    settings.data = JSON.stringify({ model: customer });
    return $.ajax(Payboard.Util.getAbsoluteUrl('/api/customers'), settings);
}

結果のリクエストは次のようになります。

POST /api/customers HTTP/1.1
Host: dev.payboard.com
Connection: keep-alive
Content-Length: 89
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://www.payboardapitest.com
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Content-Type: application/json; charset=UTF-8
Referer: http://www.payboardapitest.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

次のような POST ペイロードを使用します。

{"model":{"ExternalCustomerId":"6c5015c8-d04e-4309-a493-ec0355d1b3a0","Name":"asdfasdf"}}

呼び出される C# Web API メソッドは次のようになります。

// POST api/customers
public void Post([FromBody] CreateCustomerModel model)
{
    // Do stuff
}

次のような CreateCustomerModel を使用します。

public class CreateCustomerModel
{
    [Required]
    public string ExternalCustomerId { get; set; }

    [Required]
    public string Name { get; set; }
}

CustomerController.Post()メソッドが呼び出され、それmodel自体は null ではありませんが、プロパティExternalCustomerIdNameプロパティは null です。

私が間違っていることは明らかですか?

4

1 に答える 1