I've got a MVC 3 view which displays a list of items that can be filtered by date.
The filter is a textbox which has been jQueryUI-fied into a date picker.
View:
<%= Html.TextBoxFor(model => model.ReportedDate, new { @class = "datepicker" })%>
Script:
$(".datepicker").datepicker({
dateFormat: 'dd/mm/yy',
changeYear: true,
changeMonth: true
});
Upon a button click, I grab the value of the text box and send it to my controller action as a query string parameter of a GET request:
MyController/Search?reportedDate=30/05/2011
Controller Action:
public ActionResult Search(DateTime? reportedDate)
From this I expect the default model binder to convert the reportedDate
query parameter to a nullable DateTime (null in this context representing all dates or no filter).
However this is not the case. reportedDate
is always null. I can drill into Request.QueryString
and do the conversion manually using DateTime.TryParse
, which is my current work around, but I don't understand why it's failing in the first place.
There's no difference in the date format between the client and server and there are other (omitted here but present in the actual code) filter parameters of other data types (string and int) and they get processed without a problem.
Any suggestions why DateTime is troublesome?