0

日期时间问题。我只能将 datetime 传递到 12-12-2013,如果我传递 datetime 参数(13-12-2013 或 12-13-2013),它会显示错误(日期无效)。使用的文化是 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;
        }
    }
4

1 回答 1

0

我已经解决了这个问题。问题是客户端验证使用浏览器的文化来验证日期。因此,例如,如果将其配置为 en-US,则预期格式为 MM/dd/yyyy。如果您的服务器配置为使用 fr-FR,则预期格式为 dd/MM/yyyy。

您可以使用 web.config 中的元素来设置服务器端文化。您可以对其进行配置,使其使用与客户端相同的文化。我在 web.config 文件中更改为

<globalization culture="auto" uiCulture="auto"/>

现在一切正常

于 2013-10-17T04:47:33.407 回答