0

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>&nbsp;</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:

https://postimg.org/image/rki9qfmtz/

rukiman
  • 597
  • 10
  • 32
  • What do you mean _with different dates to break the system_? What dates are you entering? –  Jul 27 '16 at 07:52
  • I mean I will enter an end date using the picker less than start date, validation will ok as expected and block form submit. Then I will change start date to much more than end date etc...I cannot find a pattern but it is easy to break the rules so that there are cases where it fails validation when it shouldn't and allows the form to be submitted when it shouldn't. – rukiman Jul 27 '16 at 08:08
  • Edited the question to show an example of when the validation rule has failed. – rukiman Jul 27 '16 at 08:18
  • Are you using a jQuery datepicker plugin? –  Jul 27 '16 at 08:28
  • I am using the bootstrap one. Perhaps the problem maybe related to my other question see http://stackoverflow.com/questions/38607727/mvc-bootstrap-datetimepick-not-working-correctly – rukiman Jul 27 '16 at 08:31
  • Suggest you temporarily remove the plugins and test to confirm that is indeed the issue. –  Jul 27 '16 at 08:33
  • Removed the datetimepicker class from those fields which effectively made them just textfields and got into a state where startDate=1/12/2016 12:00:00 AM and enddate=31/05/2016 12:00:00 AM and the validation rule thinks is all good when it should have prompted user to correct the enddate – rukiman Jul 27 '16 at 08:42
  • Ahh its doing textual date comparison on the client...how do I fix that? – rukiman Jul 27 '16 at 08:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118397/discussion-between-stephen-muecke-and-rukiman). –  Jul 27 '16 at 08:44

0 Answers0