无法从MVC4中的UI传递Datetime。 (错误30-12-2013或12-30-2013无效日期) [英] Unable to pass Datetime from UI in MVC4. ( Error 30-12-2013 or 12-30-2013 is not valid date)
问题描述
日期时间问题。我能够将日期时间仅传递到12-12-2013,如果我通过datetime参数(13-12-2013或12-13-2013),它会显示错误(它不是有效日期)。使用的文化是en-US
我在下面给出了我的代码
Problem with Datetime . I am able to pass datetime only upto 12-12-2013 and if i pass datetime parameter (13-12-2013 or 12-13-2013) it shows me error ( It is not valid date ).Culture using is en-US
I have given my code below
< br $>
Gema_Doctor gema_Doctor = new Gema_Doctor();
[HttpPost]
公共ActionResult CreateDoctor(Gema_Doctor医生,WeekViewModel weekViewModel)
{
if(ModelState.IsValid)
{
bool isUpdated = false;
isUpdated = gema_Doctor.InsertDoctor (医生,weekViewModel);
if(isUpdated == false)
{
ViewBag.Message =False;
返回查看(GetDoctorValues());
}
返回RedirectToAction(DoctorList);
}
return View(GetDoctorValues());
}
** MasterModel.cs **
公共类Gema_Doctor:IDisposable
{
[必填]
[显示( Name =DoctorId)]
public int Dr_Id {get;组; }
[必填]
[显示(名称=公司代码)]
public int CompanyCode {get;组; }
[必填]
[显示(名称=Dr_Name)]
公共字符串Dr_Name {get;组; }
[必填]
[显示(名称=Dr_DOB)]
public DateTime Dr_DOB {get;组; }
}
DoctorServiceReference.DoctorServiceClient doctorServiceRef = new DoctorServiceReference.DoctorServiceClient();
public bool InsertDoctor(Gema_Doctor医生,WeekViewModel weekViewModel)
{
DoctorServiceReference.Doctor docRef = DoctorToDoctorSerRef(doctor,weekViewModel);
DoctorServiceReference.InsertDoctorRequest insertReq = new DoctorServiceReference.InsertDoctorRequest();
DoctorServiceReference.InsertDoctorResponse insertRes = new DoctorServiceReference.InsertDoctorResponse();
insertReq.doctor = docRef;
insertRes = doctorServiceRef.InsertDoctor(insertReq);
返回insertRes.InsertDoctorResult;
}
private DoctorServiceReference.Doctor DoctorToDoctorSerRef(Gema_Doctor医生,WeekViewModel weekVie wModel)
{
Hms.DoctorServiceReference.Doctor docRef = new Hms.DoctorServiceReference.Doctor();
docRef.CompanyCode = doctor.CompanyCode ;
docRef.Dr_Id = doctor.Dr_Id;
docRef.Dr_Name = doctor.Dr_Name;
docRef.Dr_Add1 = doctor.Dr_Add1;
docRef.Dr_Add2 = doctor.Dr_Add2;
docRef.Dr_City = doctor.Dr_City;
docRef.Dr_Pincode = doctor.Dr_Pincode;
docRef.Dr_Phone = doctor.Dr_Phone;
docRef.Dr_Cell = doctor.Dr_Cell;
docRef.Dr_DOB =(DateTime)(doctor.Dr_DOB);
docRef.Dr_Remarks = doctor.Dr_Remarks;
docRef.Activeflag = 1;
docRef.CreatedBy = 1;
docRef.CreatedDate =(DateTime)(DateTime.Now);
docRef.ModifiedBy = 1;
docRef.ModifiedDate =(DateTime)(DateTime.Now);
docRef.visitflag = 1;
if(weekViewModel.PostedDaysofWeek!= null)
{
var visitDays = DoctorVisitDays(doctor,weekViewModel);
docRef.daysun = visitDays.daysun;
docRef.daymon = visitDays .daymon;
docRef.daytue = visitDays.daytue;
docRef.daywed = visitDays.daywed;
docRef.daythu = visitDays.daythu ;
docRef.dayfri = visitDays.dayfri;
docRef.daysat = visitDays.daysat;
}
否则
{
docRef.daysun = 0;
docRef.daymon = 0;
docRef.daytue = 0;
docRef.daywed = 0;
docRef .daythu = 0;
docRef.dayfri = 0;
docRef.daysat = 0;
}
docRef.date1 =1;
docRef.Time =(DateTime)(DateTime.Now);
docRef.Morningfrm = doctor.Morningfrm;
docRef.Morningto = doctor.Morningto;
docRef.Eveningfrm = doctor.Eveningfrm;
docRef.Eveningto = doctor.Eveningto;
docRef.Active = 1;
返回docRef;
}
**我的插入服务代码**
公共布尔插入医生(医生)
{
尝试
{
使用(var dbContext = new Redim_HMSMasterEntities())
{
dbContext.Gema_Doctor.AddObject (新Gema_Doctor >
{
CompanyCode = doctor.CompanyCode,
Dr_Id = doctor.Dr_Id,
Dr_Name = doctor.Dr_Name,
Dr_Add1 = doctor.Dr_Add1,
Dr_Add2 = doctor.Dr_Add2,
Dr_City = doctor.Dr_City,
Dr_Pincode = doctor.Dr_Pincode,
Dr_Phone = doctor.Dr_Phone,
Dr_Cell = doctor.Dr_Cell,
Dr_DOB =(DateTime)(doctor.Dr_DOB) ,
Dr_Remarks = doctor.Dr_Remarks,
Activeflag = doctor.Activeflag,
CreatedBy = doctor.CreatedBy,
CreatedDate = doctor.CreatedDate,
ModifiedBy =(int)(doctor.ModifiedBy ),
ModifiedDate =(DateTime)doctor.ModifiedDate,
visitflag =(int)(doctor.visitflag),
daysun =(int )(doctor.daysun),
daymon =(int)(doctor.daymon),
daytue =(int)(doctor.daytue),
daywed =(int)(doctor.daywed),
daythu =(int)(doctor.daythu),
dayfri =(int)(doctor.dayfri ),
daysat =(int)(doctor.daysat),
date1 = doctor.date1,
时间=(日期时间)(医生) 。时间),
Morningfrm = doctor.Morningfrm,
Morningto = doctor.Morningto,
Eveningfrm = doctor.Eveningfrm,
Eveningto = doctor.Eveni ngto,
Active =(int)(doctor.Active)
});
dbContext.SaveChanges();
返回true;
}
}
catch(异常异常)
{
Debug.WriteLine(exception);
返回false;
}
}
Gema_Doctor gema_Doctor = new Gema_Doctor();
[HttpPost]
public ActionResult CreateDoctor(Gema_Doctor doctor, WeekViewModel weekViewModel)
{
if (ModelState.IsValid)
{
bool isUpdated = false;
isUpdated = gema_Doctor.InsertDoctor(doctor, weekViewModel);
if (isUpdated == false)
{
ViewBag.Message = "False";
return View(GetDoctorValues());
}
return RedirectToAction("DoctorList");
}
return View(GetDoctorValues());
}
**MasterModel.cs**
public class Gema_Doctor : IDisposable
{
[Required]
[Display(Name= "DoctorId")]
public int Dr_Id { get; set; }
[Required]
[Display(Name = "CompanyCode")]
public int CompanyCode { get; set; }
[Required]
[Display(Name = "Dr_Name")]
public string Dr_Name { get; set; }
[Required]
[Display(Name = "Dr_DOB")]
public DateTime Dr_DOB { get; set; }
}
DoctorServiceReference.DoctorServiceClient doctorServiceRef = new DoctorServiceReference.DoctorServiceClient();
public bool InsertDoctor(Gema_Doctor doctor, WeekViewModel weekViewModel)
{
DoctorServiceReference.Doctor docRef = DoctorToDoctorSerRef(doctor, weekViewModel);
DoctorServiceReference.InsertDoctorRequest insertReq = new DoctorServiceReference.InsertDoctorRequest();
DoctorServiceReference.InsertDoctorResponse insertRes = new DoctorServiceReference.InsertDoctorResponse();
insertReq.doctor = docRef;
insertRes = doctorServiceRef.InsertDoctor(insertReq);
return insertRes.InsertDoctorResult;
}
private DoctorServiceReference.Doctor DoctorToDoctorSerRef(Gema_Doctor doctor, WeekViewModel weekViewModel)
{
Hms.DoctorServiceReference.Doctor docRef = new Hms.DoctorServiceReference.Doctor();
docRef.CompanyCode = doctor.CompanyCode;
docRef.Dr_Id = doctor.Dr_Id;
docRef.Dr_Name = doctor.Dr_Name;
docRef.Dr_Add1 = doctor.Dr_Add1;
docRef.Dr_Add2 = doctor.Dr_Add2;
docRef.Dr_City = doctor.Dr_City;
docRef.Dr_Pincode = doctor.Dr_Pincode;
docRef.Dr_Phone = doctor.Dr_Phone;
docRef.Dr_Cell = doctor.Dr_Cell;
docRef.Dr_DOB = (DateTime)(doctor.Dr_DOB);
docRef.Dr_Remarks = doctor.Dr_Remarks;
docRef.Activeflag = 1;
docRef.CreatedBy = 1;
docRef.CreatedDate = (DateTime)(DateTime.Now);
docRef.ModifiedBy = 1;
docRef.ModifiedDate = (DateTime)(DateTime.Now);
docRef.visitflag = 1;
if (weekViewModel.PostedDaysofWeek != null)
{
var visitDays = DoctorVisitDays(doctor, weekViewModel);
docRef.daysun = visitDays.daysun;
docRef.daymon = visitDays.daymon;
docRef.daytue = visitDays.daytue;
docRef.daywed = visitDays.daywed;
docRef.daythu = visitDays.daythu;
docRef.dayfri = visitDays.dayfri;
docRef.daysat = visitDays.daysat;
}
else
{
docRef.daysun = 0;
docRef.daymon = 0;
docRef.daytue = 0;
docRef.daywed = 0;
docRef.daythu = 0;
docRef.dayfri = 0;
docRef.daysat = 0;
}
docRef.date1 = "1";
docRef.Time = (DateTime)(DateTime.Now);
docRef.Morningfrm = doctor.Morningfrm;
docRef.Morningto = doctor.Morningto;
docRef.Eveningfrm = doctor.Eveningfrm;
docRef.Eveningto = doctor.Eveningto;
docRef.Active = 1;
return docRef;
}
**My Service Code For Insert**
public bool InsertDoctor(Doctor doctor)
{
try
{
using (var dbContext = new Redim_HMSMasterEntities())
{
dbContext.Gema_Doctor.AddObject(new Gema_Doctor
{
CompanyCode = doctor.CompanyCode,
Dr_Id = doctor.Dr_Id,
Dr_Name = doctor.Dr_Name,
Dr_Add1 = doctor.Dr_Add1,
Dr_Add2 = doctor.Dr_Add2,
Dr_City = doctor.Dr_City,
Dr_Pincode = doctor.Dr_Pincode,
Dr_Phone = doctor.Dr_Phone,
Dr_Cell = doctor.Dr_Cell,
Dr_DOB = (DateTime)(doctor.Dr_DOB),
Dr_Remarks = doctor.Dr_Remarks,
Activeflag = doctor.Activeflag,
CreatedBy = doctor.CreatedBy,
CreatedDate = doctor.CreatedDate,
ModifiedBy = (int)(doctor.ModifiedBy),
ModifiedDate = (DateTime)doctor.ModifiedDate,
visitflag = (int)(doctor.visitflag),
daysun = (int)(doctor.daysun),
daymon = (int)(doctor.daymon),
daytue = (int)(doctor.daytue),
daywed = (int)(doctor.daywed),
daythu = (int)(doctor.daythu),
dayfri = (int)(doctor.dayfri),
daysat = (int)(doctor.daysat),
date1 = doctor.date1,
Time = (DateTime)(doctor.Time),
Morningfrm = doctor.Morningfrm,
Morningto = doctor.Morningto,
Eveningfrm = doctor.Eveningfrm,
Eveningto = doctor.Eveningto,
Active = (int)(doctor.Active)
});
dbContext.SaveChanges();
return true;
}
}
catch (Exception exception)
{
Debug.WriteLine(exception);
return false;
}
}
推荐答案
在date属性中使用[DataType(DataType.Date)]属性
use [DataType(DataType.Date)] attribute in date property
这篇关于无法从MVC4中的UI传递Datetime。 (错误30-12-2013或12-30-2013无效日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!