2016-05-24 44 views
0

我正在MVC 5中调度项目。我想检查日期选择的日期对当前日期。如果它们匹配,则仅在视图中显示“今日”日期的预定约会。目前,当我添加“appointment.AppointmentDate == DateTime.Now”时,约会不显示。而是视图重复“今天没有任命”JQuery Datetimepicker选择的日期今天不匹配

我已经通过StackOverFlow和其他网站进行了研究,试图找出没有运气。 ('#datetimepicker1.data('date'),#datetimepicker2')。datetimepicker“将类型设置为Date,但不成功。我是一名初学者,希望有人能够帮助我朝着正确的方向前进。谢谢。 我的代码如下:

MODEL:

public enum AppointmentTime 
    { 
     _1pm_to_2pm, ApplicationDbContext _dbContext = new ApplicationDbContext(); 


    public ActionResult Create(int id) 
    { 


     Property property = _dbContext.Properties.SingleOrDefault(b => b.PropertyID == id); 

     ViewBag.propertyName = property.PropertyName; 

     Consultation consultation = new Consultation(); 

     consultation.PropertyID = id; 

     return View(consultation); 

    } 


    [HttpPost] 
    public ActionResult Create(Consultation consultation) 
    { 
     try 
     { 
      if (ModelState.IsValid) 
      { 


        Property property = _dbContext.Properties.SingleOrDefault(b => b.PropertyID == consultation.PropertyID); 

        property.Consultations.Add(consultation); 
        _dbContext.SaveChanges(); 
        return RedirectToAction("Details", "Property", new { id = property.PropertyID }); 

      } 
      return View(); 
     } 
     catch 
     { 
      return View("Error"); 
     } 
    } 

     _2pm_to_3pm, 
     _3pm_to_4pm, 
     _4pm_to_5pm, 
     _5pm_to_6pm 
    } 


    public class Consultation 
    { 
     [Key] 
     public int AppointmentID { get; set; } 

     [ForeignKey("Property")] 
     public int PropertyID { get; set; } 

     [Display(Name = "Enter your name")] 
     public string AppointmentName { get; set; } 

     [Required] 
     [Display(Name = "Email")] 
     public string AppointmentEmail { get; set; } 


     [Display(Name = "Select Date")] 
     [UIHint("AppointmentDate")] 
     [Required] 
     public DateTime AppointmentDate { get; set; } 

     public AppointmentTime AppointmentTime { get; set; } 

     public virtual Property Property { get; set; } 
} 

CONTROLLOR:

[Authorize(Roles = "Admin")] 
    public ActionResult AdminAppointmentView(Consultation consultaion) 
    { 
     var appointments = _dbContext.Consultations.ToList(); 


      return View(appointments); 
    } 

CREATE VIEW

@model OpenProperties.Models.Consultation 

@{ 
    ViewBag.Title = "Create Appointment"; 
} 



<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" 
     rel="stylesheet"> 
<link rel="stylesheet" type="text/css" media="screen" 
     href="http://tarruda.github.com/bootstrap-datetimepicker/assets/css/bootstrap-datetimepicker.min.css"> 


@using (Html.BeginForm(new { enctype = "multipart/form-data", id = "form1" })) 
{ 
    @Html.ValidationSummary(true) 
    <div> 
     @Html.HiddenFor(model => model.PropertyID) 
     <br /> 

     <div class="form-group"> 
      @Html.LabelFor(m => m.AppointmentName) 
      @Html.TextBoxFor(m => m.AppointmentName, new { @class = "form-control" }) 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(m => m.AppointmentEmail) 
      @Html.TextBoxFor(m => m.AppointmentEmail, new { @class = "form-control" }) 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.AppointmentTime, new { @class = "control-label col-md-2" }) 
      @Html.EnumDropDownListFor(model => model.AppointmentTime) 
      @Html.ValidationMessageFor(model => model.AppointmentTime) 
     </div> 


     <div id="datetimepicker1" class="input-append date"> 
      @Html.TextBoxFor(m => m.AppointmentDate, "{0:dd/MM/yyyy HH}", 
     new { placeholder = "App Date", @class = "dtPicket" }) 
      <span class="add-on"> 
       <i data-time-icon="icon-time" 
        data-date-icon="icon-calendar"></i> 
      </span> 
     </div> 
     <br /> 
     <br /> 
     <input type="submit" value="Submit" /> 
    </div> 



    <script type="text/javascript" 
      src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"> 
    </script> 
    <script type="text/javascript" 
      src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js"> 
    </script> 
    <script type="text/javascript" 
      src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.min.js"> 
    </script> 
    <script type="text/javascript" 
      src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.pt-BR.js"> 
    </script> 
    <script type="text/javascript"> 
     $('#datetimepicker1, #datetimepicker2').datetimepicker({ 
      format: 'dd/MM/yyyy' 
     }); 
    </script> 
} 

AdminAppointmentView VIEW:

@model IEnumerable<OpenProperties.Models.Consultation> 

<h2>Todays Appointments</h2> 

@foreach (var appointment in Model) 
{ 
    if (appointment.AppointmentDate == DateTime.Now) 
    { 
    <li> 
     <ul>@appointment.AppointmentID</ul> 
     <ul>@appointment.AppointmentDate</ul> 
     <ul>@appointment.AppointmentEmail</ul> 
     <ul>@appointment.AppointmentName</ul> 
     <ul>For Property ID: @appointment.PropertyID</ul> 
    </li> 
    } 
    else 
    { 
     <ul> No Appointments Today </ul> 
    } 
} 

回答

0

刚刚开始一瞥。

如果(appointment.AppointmentDate == DateTime.Now) (

}

是日期格式是一样的appointment.AppointmentDate和DateTime.Now ??

如果不只是添加toString方法将这些日期。

前。

DateTime.Now.ToString("MMMM dd, yyyy") 

2)您可以检查//天,小时和分钟的差异。日期之间也是如此。而不是比较

DateTime oldDate = new DateTime(2002,7,15); 
DateTime newDate = DateTime.Now; 

// Difference in days, hours, and minutes. 
TimeSpan ts = newDate - oldDate; 
// Difference in days. 
int differenceInDays = ts.Days; 

3)您可以在datetime Picker中格式化日期,如下所示。

$('#datepicker').datetimepicker({ 
    dateFormat: "yy-mm-dd", 
    timeFormat: "hh:mm:ss" 
}); 

4)还可以请你把脚本和日期选择器代码的HTML

<script type="text/javascript" 


    src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"> 
</script> 
<script type="text/javascript" 
     src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js"> 
</script> 
<script type="text/javascript" 
     src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.min.js"> 
</script> 
<script type="text/javascript" 
     src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.pt-BR.js"> 
</script> 
<script type="text/javascript"> 
    $('#datetimepicker1, #datetimepicker2').datetimepicker({ 
     format: 'dd/MM/yyyy' 
    }); 
</script> 
相关问题