I have two date fields that I want to make sure that the end date is greater than the start date. It seems to work but not always. When testing out with different dates it stops working. I am using foolproof. Here is the code
@using (Html.BeginForm("CreateMeeting", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h3>Book a new visit</h3>
<div>The information entered below will be sent to the visitors email address @Model.Info.Email</div>
<p> </p>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.NewMeeting.Title, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.NewMeeting.Title, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewMeeting.StartTime, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.NewMeeting.StartTime, new { @class = "datetimepicker form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewMeeting.EndTime, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.NewMeeting.EndTime, new { @class = "datetimepicker form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
@Html.HiddenFor(m => m.NewMeeting.SubjectId)
@Html.HiddenFor(m => m.NewMeeting.FirstName)
@Html.HiddenFor(m => m.NewMeeting.LastName)
@Html.HiddenFor(m => m.NewMeeting.Email)
@Html.HiddenFor(m => m.NewMeeting.HostEmail)
@Html.HiddenFor(m => m.NewMeeting.HostMobile)
<input type="submit" class="btn btn-default" value="Send Invite" />
</div>
</div>
}
The model is declared
public class Meeting
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "Reason for invitation")]
public string Title { get; set; }
[Required]
[Display(Name = "Start Time")]
[DataType(DataType.DateTime)]
public DateTime StartTime { get; set; }
[Required]
[Display(Name = "End Time")]
[DataType(DataType.DateTime)]
[GreaterThan("StartTime")]
public DateTime EndTime { get; set; }
public string HostEmail { get; set; }
public string HostMobile { get; set; }
public NotificationMethod HostNotificationMethod { get; set; }
public bool HostAlreadyNotified { get; set; }
}
Added the script as a bundle
bundles.Add(new ScriptBundle("~/bundles/foolproof").Include(
"~/Client Scripts/mvcfoolproof.unobtrusive.js"));
And into the _Layout.cshtml page
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/foolproof")
Any ideas whats going on. Seems like first time I pick a date where end date is less than start date, the validation works. However when I start playing around with different dates to break the system it stops working and the form is submitted with end date less than start date. I have even got it sometimes to a state where the end date is greater than the start date but the validation still fails as it thinks the start date is greater still.
See screenshot, after playing with the end and start dates I can get into states like this: