服务器模型中未正确接收 MVC 日期



我正在尝试编辑表中已经存在的值。当我单击条目行旁边的编辑按钮时,我希望原始值的值出现在表单的输入字段中,当我更改某些内容时,应该对其进行编辑或保持不变。现在这正在工作,除了日期将 null 传递给后端控制器。我已经签入了调试器。

视图:

<div class="col-sm-12">
<div class="form-group">
<label class="col-sm-3 control-label" for="name">Date Of Birth </label>
<div class="col-sm-9">
<input type="text" id="dob" maxlength="30" ng-model="userAccount.dateOfBirth" class="form-control datepicker required" required>
</div>
</div>
</div>

棱角.js:

$scope.UserSave = function () {
debugger
$scope.userAccount.dateOfBirth = $('#dob').val();
$scope.userAccount.CountryID = $('#countryoptions').val();
Post("/User/Change", $scope.userAccount, false, $("#btnSave")).then(function (d) {
if (d.Success) {
window.location.href = "/User/LoggedIn";
}
ShowMessage(d);
});
}

通用.cs:

function Post(url, data, isBlockUI, e) {
BlockUI(isBlockUI, e);
return $.ajax({
method: "Post",
url: url,
// contentType: 'application/json',
data: data,//JSON.stringify(data),
success: function (d) {
UnBlockUI(e);
if ($.type(d) == "string" && d == "")
LogOutNotification()
else if ($.type(d) == "string")
AccessDenied();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
UnBlockUI(e);
ErrorMessage(errorMsg);
}
});

}

我做了一个单独的函数来处理从后端发送和接收的数据,如上所示,我知道它工作正常,因为我正在使用它将数据添加到数据库中。

但现在我的问题来了。此数据被传递给控制器,并且传递的数据除了日期之外都是正常的。日期为空,我不知道为什么。由于输入数据时数据库中的日期字段设置为不为空,因此它给了我一个例外。

控制器:

public JsonResult Change(Account account) {
return Json((new userLogic()).Change(account), JsonRequestBehavior.AllowGet);
}

在这里,在调试时,我检查 Account.DateOfbirth 属性的值及其设置为 1/1/0001,据我所知,该值在日期格式中为 null。 因此,尝试保存到数据库时出现错误。

逻辑:

public Message Change(Account account) {
Message msg = new Message();
try
{
Account userProfile = db.Accounts.Where(b => b.Email == account.Email).FirstOrDefault();
var dob = account.dateOfBirth;
if (userProfile == null)
{
msg.Success = false;
msg.MessageDetail = "somehting went wrong could not find the data entry amke sure the database connection is working";
}
else {
userProfile.firstName = account.firstName;
userProfile.lastName = account.lastName;
userProfile.Email = account.Email;
userProfile.password = account.password;
userProfile.CountryID = account.CountryID;
userProfile.dateOfBirth = account.dateOfBirth;
userProfile.phoneNo = account.phoneNo;
db.Entry(userProfile).State = EntityState.Modified;
db.SaveChanges();
msg.Success = true;
msg.MessageDetail = "Edit successful";
}
}
catch (Exception ex) {
msg.Success = false;
msg.MessageDetail = "somehting went wrong could not find the data entry make sure the database connection is working";
} 
return msg;
}

我知道当我将对象的数据发送到控制器时,问题出在某个地方。但是我似乎找不到我的错误,我以类似的方式添加数据只是更改后端中的逻辑,并且似乎工作正常。

来自浏览器的数据 网络 选项卡显示 AJAX 请求中发送的内容:

编码的网址:

IsPermissionUpdated: false
UserId: 12
password: 88888888
firstName: Ali
lastName: Shujaat
Email: Ali%40gmail.com
CountryID: 6
phoneNo: 3244183346
dateOfBirth: 28%2F08%2F2018
isActive: true
Country: 
Password: 97533284

解码的网址:

IsPermissionUpdated: false
UserId: 12
password: 88888888
firstName: Ali
lastName: Shujaat
Email: Ali@gmail.com
CountryID: 6
phoneNo: 3244183346
dateOfBirth: 28/08/2018
isActive: true
Country: 
Password: 97533284

添加数据的代码:因为我使用的是完全相同的方法并且工作正常。

视图:

<div class="col-sm-12">
<div class="form-group">
<label class="col-sm-3 control-label" for="name">Date Of Birth </label>
<div class="col-sm-9">
<input type="text" id="dob" maxlength="30" ng-model="userAccount.dateOfBirth" class="form-control datepicker required" required>
</div>
</div>
</div>

角:

$scope.UserSave = function () {
$scope.userAccount.dateOfBirth = $('#dob').val();
$scope.userAccount.CountryID = $('#countryoptions').val();
Post("/User/Save",  $scope.userAccount , false, $("#btnSave")).then(function (d) {
if (d.Success) {
}
ShowMessage(d);
});
}

控制器:

public JsonResult Save(Account account) {
return Json((new userLogic()).Save(account), JsonRequestBehavior.AllowGet);  
}

逻辑.cs:

public Message Save(Account account)
{
Message msg = new Message();
// Account userProfile = Static.UserProfile;
try
{
Account foundaccount = db.Accounts.SingleOrDefault(x => x.Email == account.Email);
if (foundaccount != null)
{
msg.Success = false;
msg.MessageDetail = "Email already exist.";
}
else
{
Country c1 = db.Countries.Find(account.CountryID);
Account newaccount = new Account();
//newaccount.UserId = account.UserId;
newaccount.firstName = account.firstName;
newaccount.lastName = account.lastName;
newaccount.Email = account.Email;
newaccount.password = account.password;
newaccount.phoneNo = account.phoneNo;
newaccount.dateOfBirth = account.dateOfBirth;
newaccount.CountryID = account.CountryID;
newaccount.isActive = true;
//newaccount.Country = c1;
db.Accounts.Add(newaccount);
//db.Entry(newaccount).State = EntityState.Added;
db.SaveChanges();
msg.MessageDetail = account.Email + "has been saved";    
}
//userProfile.LogActivity("Save", msg.MessageDetail, "EmploymentType");
}
catch (Exception ex)
{
msg.Success = false;
msg.MessageDetail = Message.ErrorMessage;
// userProfile.LogError(ex, "BLL/EmploymentTypeLogic/Save");
}
return msg;
}
}

我得到的异常:

Message = "The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.rnThe statement has been terminated."

我发现了问题。Jao和Adyson很清楚错误是什么。日期返回到控制器的格式错误。每次我选择格式为 dd-mm-yyyy 的日期时,dd 大于 13 都会给我一个错误,低于该错误的所有内容都有效,因为 c# 中的日期时间是 mm-dd-yyyyy。这就是我收到错误的原因。

$scope.UserSave = function () {
//debugger
var convertedDate = $('#dob').val();
var splitdate = convertedDate.split('/');
var year1 = splitdate[2];
var month1 = splitdate[1];
var day1 = splitdate[0];
var date = month1 + "/" + day1 + "/" + year1;
$scope.userAccount.dateOfBirth = date;
$scope.userAccount.CountryID = $('#countryoptions').val();
Post("/User/Change", $scope.userAccount, false, $("#btnSave")).then(function (d) {
if (d.Success) {
window.location.href = "/User/LoggedIn";
}
ShowMessage(d);
});
}

将日期格式化为 mm/dd/yyyy 可以完全解决问题。需要注意的另一件事是,首先,因为我从表中获取数据作为 json 数据。Json 以不同的格式存储日期,因此,如果我没有编辑日期字段。格式将完全不同,并且将引发相同的异常。因此,在显示数据之前格式化数据是更好的选择。

最新更新