我正在使用angular with entity framework将数据保存到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#中的Controller
[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
});
你知道为什么会这样吗?
感谢
答案 0 :(得分:0)
好的,我通过将DateTime字段的声明从'DateTime'更改为'DateTime?'来修复它。
[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
});