【发布时间】:2014-12-20 20:32:26
【问题描述】:
在我的 WebAPI asp.net mvc 控制器 Delete 方法中,传入的名为 contact 的对象结果为空。我到处检查了我的代码,但我无法找出根本原因。我的编辑操作以非常相似的方式成功运行。
那么是什么原因导致 webapi asp.net 方法中的联系人对象参数以空值出现?
如图所示,我已经检查了角度控制器内的联系人对象在传递给 webapi 删除方法之前不为空。
Here is my rest of the code
<div data-ng-controller="ContactDeleteController">
<form name ="deleteContact" data-ng-submit="saveDeleteContact()">
<div>
<label>First Name: </label>
<input required type="text" placeholder="Enter First Name" data-ng-model="contact.FirstName"/>
</div>
<div>
<label>Last Name: </label>
<input required type="text" placeholder="Enter Last Name" data-ng-model="contact.LastName"/>
</div>
<div>
<label>Email Address: </label>
<input required type="text" placeholder="Enter Email Address" data-ng-model="contact.EmailAddress"/>
</div>
<div>
<label>Cell Phone Number: </label>
<input required type="text" placeholder="Enter Phone Number" data-ng-model="contact.PhoneNumber"/>
</div>
<div></div>
<div>
<button class="btn btn-primary" type="submit">Delete</button>
</div>
</form>
</div>
var ContactDeleteController = function ($scope, $http, $location) {
var contactId = $location.absUrl().match(/\/Delete\/(.*)/)[1];
$http.get("/api/ContactWeb/" + contactId)
.then(function (response) {
$scope.contact = response.data;
});
$scope.saveDeleteContact = function () {
var con = $scope.contact;
$http.delete("/api/ContactWeb", con)
.then(function (response) {
$scope.contact = response.data;
});
window.location = "/Contact/Index";
};
};
【问题讨论】:
-
与其附加图片,不如直接复制粘贴代码会更有帮助。图像不可见。此外,人们无法复制它。
-
您可以尝试注释掉 window.location="/Contact/Index" 行,看看会发生什么。想知道它是否在 $http 设置后立即被调用(而不是在 $http 完成时在 .then 块内)是否导致竞争条件导致 $http 无法正常完成。
标签: asp.net-mvc angularjs asp.net-web-api