Asp.net mvc 4 无法从UI传递Datetime的参数

Asp.net mvc 4 无法从UI传递Datetime的参数,asp.net-mvc-4,Asp.net Mvc 4,日期时间有问题。我只能在2013年12月12日之前传递datetime,如果传递datetime参数(2013年12月13日或2013年12月13日),则会显示错误(日期无效)。使用的区域性为en US 我在下面给出了我的代码 代码: ** my view page is** @model Hms.ViewModels.DoctorModel @{ ViewBag.Title = "CreateDoctor"; Layout = "~/Views/Shared/_Layo

日期时间有问题。我只能在2013年12月12日之前传递datetime,如果传递datetime参数(2013年12月13日或2013年12月13日),则会显示错误(日期无效)。使用的区域性为en US 我在下面给出了我的代码

代码:

 ** my view page is** 
 @model Hms.ViewModels.DoctorModel
 @{
    ViewBag.Title = "CreateDoctor";
    Layout = "~/Views/Shared/_LayoutMaster.cshtml";
  }

  <span class="title">CreateDoctor</span>
  <section class="content-innerPage">
  @using (Html.BeginForm())
{

 @Html.ValidationSummary(true)

 <table class="doctortable">

    <tr>
           <td>
             @Html.LabelFor(M => Model.Doctor.Dr_Id)
             @Html.TextBoxFor(M => Model.Doctor.Dr_Id)
             @Html.ValidationMessageFor(model => model.Doctor.Dr_Id)
           </td>
           <td>
             @Html.LabelFor(M => Model.Doctor.CompanyCode)
             @Html.TextBoxFor(M => Model.Doctor.CompanyCode)
             @Html.ValidationMessageFor(model => model.Doctor.CompanyCode)
           </td>
           <td>
             @Html.LabelFor(M => Model.Doctor.Dr_Name)
             @Html.TextBoxFor(M => Model.Doctor.Dr_Name)
              @Html.ValidationMessageFor(model => model.Doctor.Dr_Name)
           </td>
   </tr>
   <tr>
           <td>
             @Html.LabelFor(M => Model.Doctor.Dr_Add1)
             @Html.TextBoxFor(M => Model.Doctor.Dr_Add1)

           </td>
           <td>
             @Html.LabelFor(M => Model.Doctor.Dr_Add2)
             @Html.TextBoxFor(M => Model.Doctor.Dr_Add2)
           </td>
           <td>
             @Html.LabelFor(M => Model.Doctor.Dr_City)
             @Html.TextBoxFor(M => Model.Doctor.Dr_City)
           </td>
   </tr>
   <tr>
           <td>
             @Html.LabelFor(M => Model.Doctor.Dr_Pincode)
             @Html.TextBoxFor(M => Model.Doctor.Dr_Pincode)

           </td>
           <td>
             @Html.LabelFor(M => Model.Doctor.Dr_Phone)
             @Html.TextBoxFor(M => Model.Doctor.Dr_Phone)
           </td>
           <td>
             @Html.LabelFor(M => Model.Doctor.Dr_Cell)
             @Html.TextBoxFor(M => Model.Doctor.Dr_Cell)
           </td>
   </tr>
   <tr>
           <td>
             @Html.LabelFor(M => Model.Doctor.Dr_DOB)
             @Html.TextBoxFor(M => Model.Doctor.Dr_DOB) 
             @Html.ValidationMessageFor(M => Model.Doctor.Dr_DOB)

           </td>
           <td>
             @Html.LabelFor(M => Model.Doctor.Morningfrm)
             @Html.TextBoxFor(M => Model.Doctor.Morningfrm)
           </td>
           <td>
             @Html.LabelFor(M => Model.Doctor.Morningto)
             @Html.TextBoxFor(M => Model.Doctor.Morningto)
           </td>
   </tr>
    <tr>
           <td>
             @Html.LabelFor(M => Model.Doctor.Eveningfrm)
             @Html.TextBoxFor(M => Model.Doctor.Eveningfrm)

           </td>
           <td>
             @Html.LabelFor(M => Model.Doctor.Eveningto)
             @Html.TextBoxFor(M => Model.Doctor.Eveningto)
           </td>
           <td></td>
   </tr>
   </table>




<div class="checkboxforweek">
@Html.CheckBoxListFor(M => Model.WeekViewModel.PostedDaysofWeek.DayIDs, M => Model.WeekViewModel.AvailableDays, c => c.week_Id, c => c.weeK_Name,
         M => Model.WeekViewModel.SelectedDays, Position.Horizontal)
</div>


    <p>
        <input type="submit" value="Create" />
    </p>

}

 <div>
 @Html.ActionLink("Back to List", "DoctorList")
 </div>
  </section>
 @section Scripts {
 @Scripts.Render("~/bundles/jqueryval")

 }




 **MyController Code:**

  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;
        }
    }
**我的查看页面是**
@模型Hms.ViewModels.DoctorModel
@{
ViewBag.Title=“CreateDoctor”;
Layout=“~/Views/Shared/\u LayoutMaster.cshtml”;
}
创世医生
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
@LabelFor(M=>Model.Doctor.Dr\u Id)
@Html.TextBoxFor(M=>Model.Doctor.Dr_Id)
@Html.ValidationMessageFor(model=>model.Doctor.Dr\u Id)
@LabelFor(M=>Model.Doctor.CompanyCode)
@Html.TextBoxFor(M=>Model.Doctor.CompanyCode)
@Html.ValidationMessageFor(model=>model.Doctor.CompanyCode)
@LabelFor(M=>Model.Doctor.Dr_Name)
@Html.TextBoxFor(M=>Model.Doctor.Dr_Name)
@Html.ValidationMessageFor(model=>model.Doctor.Dr_Name)
@LabelFor(M=>Model.Doctor.Dr_Add1)
@Html.TextBoxFor(M=>Model.Doctor.Dr_Add1)
@LabelFor(M=>Model.Doctor.Dr_Add2)
@Html.TextBoxFor(M=>Model.Doctor.Dr_Add2)
@LabelFor(M=>Model.Doctor.Dr_City)
@Html.TextBoxFor(M=>Model.Doctor.Dr_City)
@LabelFor(M=>Model.Doctor.Dr_Pincode)
@Html.TextBoxFor(M=>Model.Doctor.Dr_Pincode)
@LabelFor(M=>Model.Doctor.Dr\u电话)
@Html.TextBoxFor(M=>Model.Doctor.Dr_电话)
@LabelFor(M=>Model.Doctor.Dr\u单元格)
@Html.TextBoxFor(M=>Model.Doctor.Dr\u单元格)
@LabelFor(M=>Model.Doctor.Dr_-DOB)
@Html.TextBoxFor(M=>Model.Doctor.Dr_-DOB)
@Html.ValidationMessageFor(M=>Model.Doctor.Dr_-DOB)
@LabelFor(M=>Model.Doctor.Morningfrm)
@Html.TextBoxFor(M=>Model.Doctor.Morningfrm)
@LabelFor(M=>Model.Doctor.Morningto)
@Html.TextBoxFor(M=>Model.Doctor.Morningto)
@LabelFor(M=>Model.Doctor.Eveningfrm)
@Html.TextBoxFor(M=>Model.Doctor.Eveningfrm)
@LabelFor(M=>Model.Doctor.Eveningto)
@Html.TextBoxFor(M=>Model.Doctor.Eveningto)
@(M=>Model.WeekViewModel.PostedDaysofWeek.DayIDs,M=>Model.WeekViewModel.AvailableDays,c=>c.week\u Id,c=>c.week\u Name,
M=>Model.WeekViewModel.SelectedDays,Position.Horizontal)

} @ActionLink(“返回列表”、“医生列表”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) } **MyController代码:** Gema_医生Gema_医生=新的Gema_医生(); [HttpPost] 公共行动结果CreateDoctor(Gema_Doctor Doctor,WeekViewModel WeekViewModel) { if(ModelState.IsValid) { bool isUpdated=false; isUpdated=gema_Doctor.InsertDoctor(Doctor,weekViewModel); 如果(isUpdated==false) { ViewBag.Message=“False”; 返回视图(GetDoctorValues()); } 返回重定向操作(“医生列表”); } 返回视图(GetDoctorValues()); } **MasterModel.cs** 公立Gema_医生:我可以 { [必需] [显示(Name=“DoctorId”)] 公共int Dr_Id{get;set;} [必需] [显示(Name=“CompanyCode”)] 公共int公司代码{get;set;} [必需] [显示(Name=“Dr_Name”)] 公共字符串dru Name{get;set;} [必需] [显示(Name=“Dr_DOB”)] 公共日期时间Dr_DOB{get;set;} } DoctorServiceReference.DoctorServiceClient doctorServiceRef=新的DoctorServiceReference.DoctorServiceClient(); 公共bool InsertDoctor(Gema_博士,WeekViewModel WeekViewModel) { DoctorServiceReference.DoctorDocRef=DoctorToDoctorSerRef(医生,周视图模型); DoctorServiceReference.InsertDoctorRequest insertReq=新建DoctorServiceReference.InsertDoctorRequest(); DoctorServiceReference.InsertDoctorResponse insertRes=新建DoctorServiceReference.InsertDoctorResponse(); insertReq.doctor=docRef; insertRes=doctorservice.InsertDoctor(insertReq); 返回insertRes.InsertDoctorResult; } 私人医生服务参考。医生医生医生TodoctorSerRef(Gema_医生医生,WeekViewModel WeekViewModel) { Hms.DoctorServiceReference.Doctor docRef=新的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=docRef.Dr_City; docRef.Dr_Pincode=doctor.Dr_Pincode; docRef.Dr_Phone=docRef.Dr_Phone; docRef.Dr_Cell=doctor.Dr_Cell; docRef.Dr_DOB=(日期时间)(doc.Dr_DOB); docRef.Dr_备注=docRef.Dr_备注; 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(医生,周视图模型); docRef.daysun=visitDays.daysun; docRef.daymon=visitDays.daymon;
<globalization culture="auto" uiCulture="auto"/>