C# 服务器型号中未正确接收MVC日期
我正在尝试编辑表中已存在的值。当我单击条目行旁边的编辑按钮时,我希望表单上的输入字段中显示原始值,并且当我更改某些内容时,应该对其进行编辑或保持不变。现在,除了将空值传递给后端控制器的日期外,这一切正常。我已签入调试器 查看:C# 服务器型号中未正确接收MVC日期,c#,asp.net,angularjs,asp.net-mvc,datetime,C#,Asp.net,Angularjs,Asp.net Mvc,Datetime,我正在尝试编辑表中已存在的值。当我单击条目行旁边的编辑按钮时,我希望表单上的输入字段中显示原始值,并且当我更改某些内容时,应该对其进行编辑或保持不变。现在,除了将空值传递给后端控制器的日期外,这一切正常。我已签入调试器 查看: <div class="col-sm-12"> <div class="form-group"> <label class="col-sm-3 control-label" for="name">Date
<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 () {
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);
});
}
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);
}
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;
}
<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);
}
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.\r\nThe statement has been terminated."
Generic.cs:
<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 () {
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);
});
}
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);
}
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;
}
<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);
}
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.\r\nThe statement has been terminated."
}
我制作了一个单独的函数来处理从后端发送和接收的数据,如上图所示,我知道它可以正常工作,因为我正在使用它将数据添加到数据库中
但现在我的问题来了。此数据被传递给控制器,并且传递的数据除了日期之外都是正确的。日期是空的,我不知道为什么。由于在输入数据时,数据库中的日期字段被设置为NOTNULL,因此它给了我一个异常
控制器:
<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 () {
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);
});
}
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);
}
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;
}
<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);
}
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.\r\nThe statement has been terminated."
在这里调试时,我检查account.dateofbirth属性的值,并将其设置为1/1/0001,据我所知,该值在日期格式中为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>
$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);
});
}
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);
}
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;
}
<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);
}
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.\r\nThe statement has been terminated."
当我将对象的数据发送到控制器时,我知道问题出在某个地方。但我似乎找不到我的错误,我正在以类似的方式添加数据,只是在后端更改了逻辑,它似乎工作正常
“浏览器网络”选项卡中的数据显示AJAX请求中发送的内容:
URL编码为:
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
已解码的URL:
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 () {
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);
});
}
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);
}
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;
}
<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);
}
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.\r\nThe statement has been terminated."
控制器:
<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 () {
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);
});
}
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);
}
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;
}
<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);
}
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.\r\nThe statement has been terminated."
Logic.cs:
<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 () {
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);
});
}
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);
}
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;
}
<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);
}
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.\r\nThe statement has been terminated."
我得到的例外情况:
<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 () {
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);
});
}
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);
}
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;
}
<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);
}
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.\r\nThe statement has been terminated."
在MVC视图中,当您将日期直接呈现到HTML元素(绕过模型状态)中并随后传递给操作时,请使用ISO日期格式MyDate.ToString(“o”)。当发回时,MVC将正确地将日期解释回控制器操作中 使用隐藏字段和Ajax的示例
@Html.Hidden("SelectedMonth", Model.SelectedMonth.ToString("o"), new { @id = "hfSelectedMonth" })
JavaScript示例
//-----------------------------------------
// the month being viewed
var selectedMonth = $("#hfSelectedMonth").val();
//-----------------------------------------
// get content from URL using Ajax
// Note use of TRADITIONAL is required to pass the array to get a list of strings at the server
// ACTION with selected users parameter used
//-----------------------------------------
$.ajax({
url: "/SomeController/SomeAction/",
type: "get",
traditional: true,
data: { selectedMonth: selectedMonth, selectedUserIds: selectedUserIds },
success: function (result) {
$("#divWhereTheResultGoes").html(result);
}
});
我发现了问题。Jao和ADyson很清楚错误的原因。日期返回控制器的格式错误。每次我选择dd-mm-yyyy格式的日期,其中dd大于13,都会给我一个错误,下面的所有内容都有效,因为c#中的日期时间是mm-dd-yyyy。这就是我出错的原因
$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以不同的格式存储日期,因此如果我没有编辑日期字段。格式将完全不同,并且将引发相同的异常。因此,在显示数据之前格式化数据是一个更好的选择 发回日期的格式是什么?您(作为一个测试)是否可以尝试以yyyy-mm-dd格式发送硬编码日期?dateOfBirth={1/1/0001 12:00:00 AM}这是我将尝试发送硬编码日期的格式即使硬编码日期是以01/01/0001格式发送的try
var dob='2018-01-01'代码>我不确定我是否理解您的方法。你是说我需要在将日期发送到控制器之前将其转换为字符串,以便控制器能够正确解析它吗?因为我正在做的是将日期值作为字符串,并将其在json对象中传递给控制器,然后控制器应该能够正确解析它。但是,只要我调用控制器中的对象,日期值就已经是01/01/0001。我猜问题在于将数据发送到控制器。