【发布时间】:2017-03-16 21:47:18
【问题描述】:
我正在使用 Angular 和实体框架将数据保存到 SQL 后端数据库。
当我完成网络表单中的所有字段时,它会保存到数据库中。如果我将一个字段留空(这不是必需的),那么所有项目都不会保存到数据库中。数据库列都可以为空。
这里是 JS:
$scope.saveData = function () {
$http.post("/AddTeam/SaveData", {
recordID: $scope.formGuid,
GenAddress1: $scope.formData.GenAddress1,
GenAddress2: $scope.formData.GenAddress2,
GenAddress3: $scope.formData.GenAddress3,
GenCity: $scope.formData.GenCity,
GenPostCode: $scope.formData.GenPostCode,
GenPhoneNo: $scope.formData.GenPhoneNo,
GenFaxNo: $scope.formData.GenFaxNo,
TodaysDate: $scope.TodaysDate,
GenDeactivationDate: $scope.formData.GenDeactivationDate
})
.success(function (result) {
$scope.teams = result;
})
.error(function (data) {
console.log(data);
});
}
这是 C# 中的控制器
[HttpPost]
public void SaveData(string recordID, string GenAddress1, string GenAddress2, string GenAddress3, string GenCity,
string GenPostCode, string GenPhoneNo, string GenFaxNo, DateTime TodaysDate, DateTime GenDeactivationDate)
{
var db = new MYDBEntities();
db.WebForms_MyTable.Add(new WebForms_MyTable()
{
RecordID = recordID,
AddressLine1 = GenAddress1,
AddressLine2 = GenAddress2,
AddressLine3 = GenAddress3,
City = GenCity,
Postcode = GenPostCode,
PhoneNumber = GenPhoneNo,
FaxNumber = GenFaxNo,
ActivationDate = TodaysDate,
DeactivationDate = GenDeactivationDate
});
你知道为什么会这样吗?
谢谢
【问题讨论】:
-
你试过在你的 C# 代码中设置断点吗?
-
在调用后尝试在 JS 中打印值。看看它们是否打印正确。而且,当您将其中一个字段留空时,您确定还会进行邮寄电话吗?
-
刚刚在我的 c# 中添加了一个断点 - 当我完成所有字段时它命中 c# 时很有趣。当我将字段留空时,它不会击中 c#...所以也许我在我的 JS 中遗漏了一些东西....
-
好的,我已经了解它是什么 - 当 DateTime 字段为空时,JS 不会将其传递给 c# 控制器.. 当文本字段为空时,它会.. .. 嗯....
标签: c# angularjs asp.net-mvc entity-framework