I am using foolproof in a MVC project. I am having trouble where it seems like it is doing a textual comparison to figure out whether the end datetime is greater than the start datetime.
Here is the model:
public class TestModel
{
[Required]
[DataType(DataType.DateTime)]
public DateTime start { get; set; }
[Required]
[DataType(DataType.DateTime)]
[GreaterThan("start")]
public DateTime end { get; set; }
}
the view
@model WebApplication1.Models.TestModel
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm("Index2", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.start, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.start, "{0:dd/MM/yyyy HH:mm}", new { @class = "form-control datetimepicker" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.end, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.end, "{0:dd/MM/yyyy HH:mm}", new { @class = "form-control datetimepicker" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Set password" class="btn btn-default" />
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
The controller
public ActionResult Index()
{
TestModel model = new TestModel();
// model.start = new DateTime(2016, 5, 31);
//model.end = new DateTime(2016, 8, 31);
model.start = DateTime.Now;
model.end = DateTime.Now.AddMinutes(30);
return View(model);
}
You can download my project from to try out: https://www.dropbox.com/s/pf0lkg297hq0974/WebApplication1.zip?dl=0
There are two issues: 1) When you make start date 11/07/2016 23:51 and end date 02/08/2016 00:21 you get a validation error as it thinks the end date is less than the start date. It seems like a textual comparison to me.
2) Also if you un-comment out the two model.start and model.end date initialization statements you get invalid date when submitting.
Note I was using bootstrap datetimepicker but commented out the bit where it initialises it when the document is ready. I thought it had something to do with the issue but seems not. Eventually I want to have datetimepicker working as well.
Also note I am in Australia so the date format is dd/mm/yyyy