我试图从我的网页上发布数据到我的RegistrationController Api。我在提交表单时看到它,但是当我在WebApi上打断点时,参数'pilot'为空。有人能告诉我,我需要做什么才能让数据从网页传递到Registration ApiController?将JSON对象传递给WebApi
我看到这个post,但它似乎并没有为我工作。这post,这是接近我的问题,提到该模型需要注释,但仍然没有为我工作。
按钮CSHTML页:
<button id="submitRegistration" class="btn btn-lg btn-primary btn-block"
type="submit">Submit</button>
角这个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; }
}
删除JSON.stringify()做到了。我以为我需要那个。我也看到我没有DataContract或DataMember属性。谢谢。 – Wannabe