RegistrationController

RegistrationController

我正在尝试将网页中的数据发布到我的RegistrationController Api。提交表单时可以看到它,但是当我在WebApi上遇到断点时,参数“ pilot”为null。有人可以告诉我我需要做什么才能使数据从网页传递到Registration ApiController吗?

我看到了这个post,但它似乎对我没有用。这个与我的问题很接近的post提到该模型需要注释,但仍然对我不起作用。

cshtml页面上的按钮:

<button id="submitRegistration" class="btn btn-lg btn-primary btn-block"
    type="submit">Submit</button>


Angular RegistrationController:

<script type="text/javascript">
'use strict';

var sampleApp = angular.module('RegistrationApp', []);

sampleApp.controller('RegistrationController', function ($scope, $http) {
    var registrationData = {
        "firstname": $scope.firstname,
        "lastname": $scope.lastname,
        "faaNumber": $scope.faaNumber
    };

    $('#submitRegistration').click(function () {

        registrationData = {
            FirstName: $scope.firstname,
            LastName: $scope.lastname,
            FAANumber: $scope.faaNumber
        };

       // When I hit this debugger, the data is good.
       debugger;
        $.post("api/registration",
                JSON.stringify(registrationData),
                function (value) {
                $('#registrationMessage').append(value);
                },
                "json"
        );
    });
});
</script>


WebApi RegistrationController类:

public class RegistrationController : ApiController
{
// *********** Pilot is null when the breakpoint hits here ************
public HttpResponseMessage Post([FromBody]PilotModel pilot)
{
    this.RegisterPilot(pilot);

    var response = Request.CreateResponse<PilotModel>(HttpStatusCode.Created, pilot);

    return response;
}

private string RegisterPilot(PilotModel pilot)
{
    var result = string.Empty;

    ...

    return result;
}
}


飞行员模型:

[DataContract]
public class PilotModel
{
    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }

    [DataMember]
    public string FAANumber { get; set; }
}

最佳答案

仅需一分钟,请尝试一下:

$.post("api/registration",
                registrationData,
                function (value) {
                $('#registrationMessage').append(value);
                },
                "json"
        );

10-04 21:21