{ 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无效日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-18 18:34