所选的JQuery Datetimepicker日期与今天的日期不匹配

时间:2016-05-24 19:04:58

标签: asp.net-mvc twitter-bootstrap datetimepicker

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

我已经通过StackOverFlow和其他网站进行了研究,试图弄清楚没有运气。一次尝试是在创建视图中添加“.data('date'):”$('#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);
    }

创建视图

@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>
    }
}

1 个答案:

答案 0 :(得分:0)

刚开始一瞥。

if(appointment.AppointmentDate == DateTime.Now) (

}

对于appointment.AppointmentDate和DateTime.Now ??日期格式是否相同。

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

离。

Externalizable

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

Serializable

3)您可以在日期时间选择器中格式化日期,如下所示。

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

4)你也可以把脚本和日期选择代码放在html

之前
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;