我有两个日期字段,我想确保结束日期大于开始日期。它似乎工作但并不总是。在使用不同日期进行测试时,它会停止工作。我使用万无一失。这是代码
@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>
}
宣布模型
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; }
}
将脚本添加为包
bundles.Add(new ScriptBundle("~/bundles/foolproof").Include(
"~/Client Scripts/mvcfoolproof.unobtrusive.js"));
进入_Layout.cshtml页面
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/foolproof")
任何想法都在发生。好像我第一次选择结束日期小于开始日期的日期,验证工作。然而,当我开始玩不同的日期来打破系统时,它会停止工作,并且提交的表单的结束日期小于开始日期。我有时甚至得到结束日期大于开始日期的状态,但验证仍然失败,因为它认为开始日期仍然更大。
查看截图,在玩完结束日期后我可以进入这样的状态: