I'm trying to post data from my web page to my RegistrationController Api. I see it when I submit the form, but when I hit my breakpoint on the WebApi, the parameter 'pilot' is null. Can someone tell me what I need to do to get the data to pass from the web page over to the Registration ApiController?
I saw this post, but it did not seem to work for me. This post, which was close to my question, mentioned that the model needed to annotated, but still did not work for me.
Button on cshtml page:
<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 Class:
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;
}
}
Pilot Model:
[DataContract]
public class PilotModel
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public string FAANumber { get; set; }
}