I'm trying to use the jQuery datepicker within my MVC application, but every time I submit the form, something goes wrong; may it be a wrong conversion of the date, or simply null
.
It's difficult to explain, so I'd rather show in code.
Controller function
[HttpPost]
public ActionResult GetData(ReportViewModel model)
{
using (var eh = new ExcelHelper())
{
var data = new List<ReportDataModel>();
if (!model.SpecificTimeline) { //Do something! }
else if (model.SpecificTimeline) { //Do something else! }
return View("Index", model);
}
}
Controller index
public ActionResult Index(ReportViewModel model = null)
{
using (var eh = new ExcelHelper())
{
if (model.Data == null)
{
model = new ReportViewModel(false, DateTime.Now.AddMonths(-1), DateTime.Now, eh.GetRawReportData());
}
return View(model);
}
}
Markup (HTML)
<div>
@using (Html.BeginForm("GetData", "Report", FormMethod.Post))
{
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.SpecificTimeline, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.CheckBoxFor(model => model.SpecificTimeline, new { @class = "" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartDate, new { @class = "control-label col-md-2 " })
<div class="col-md-10">
@Html.TextBoxFor(model => model.StartDate, new { @class = "form-control datepicker" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, new { @class = "control-label col-md-2 " })
<div class="col-md-10">
@Html.TextBoxFor(model => model.EndDate, new { @class = "form-control datepicker" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Generate Report" class="btn btn-primary" />
</div>
</div>
</div>
}
</div>
JS (Only used to convert the Datetime to the correct format)
@using (Html.BeginScripts())
{
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/themes/base/jqueryui-datepicker")
<script>
$(function () {
var startDate = new Date('@Model.StartDate');
var endDate = new Date('@Model.EndDate');
$('.start-date').datepicker({
dateFormat: 'mm-dd-yy'
}).datepicker('setDate', startDate);
$('.end-date').datepicker({
dateFormat: 'mm-dd-yy'
}).datepicker('setDate', endDate);
});
</script>
}
Quick look at the model
public class ReportViewModel
{
public ReportViewModel() { }
public ReportViewModel(bool specificTimeline, DateTime startDate, DateTime endDate, List<ReportDataModel> data)
{
SpecificTimeline = specificTimeline;
StartDate = startDate;
EndDate = endDate;
Data = data;
}
public bool SpecificTimeline { get; set; }
[DataType(DataType.DateTime)]
public DateTime StartDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime EndDate { get; set; }
public List<ReportDataModel> Data { get; set; }
}
Problem
Whenever I select to dates;
startDate: 17-02-2016
endDate: 22-02-2016
format: dd-mm-yy
and I press the submit button "Generate Report", I want the function GetData(ReportViewModel model)
to be hit, and generate the parameter model
with the correct data. This is not happening as the EndDate
property is 01-01-0001
, even though I selected a date from the datepicker.
Edit
I have noticed that the DateTime
properties are equal to 01-01-0001
if I don't select anything in the datepicker input. If I do select a new date in both fields, the date in correct in the model that generates when the function Getdata()
is hit!
So if I do not touch the inputs, the date gets set to the default 01-01-0001
.
Any workaround to that?