jQuery DateTime picker and ASP.NET MVC

asp.net-mvc, datetime, jquery

Solution

This usually happens when the input parameter to the controller action is not really a DateTime object.

Double check that the name of your input parameter to the controller action is "TimeAndDate" and that the type is String.

You can then use DateTime.Parse(String) to parse the string into the DateTime type.

Problem

I'm currently using a jQuery Date Time picker to select a date time to be put in to a database. When using the date time picker, the result shows up properly in the text box it is bound to (IE 27/09/2009 16:00). However, the date time is not being passed to the MVC application properly, and is being received as 01/01/0001 00:00:01. The method dealing with this requires a single paramaeter - Match m. The page is strongly typed as a match. ``` <p> <label for="TimeAndDate">Time and date (click to reveal date and time picker):</label> <br /> <%= Html.TextBox("TimeAndDate") %> <%= Html.ValidationMessage("TimeAndDate", "*") %> </p> <script type="text/javascript"> $(function() { $('#TimeAndDate').datepicker({ duration: '', showTime: true, constrainInput: false }); }); </script> ``` For lengths sake, I've omitted the script includes above, but they are present in the page. The text box and validation message fields were generated by visual studio. I have a feeling I need, somehow, to implicitly convert the string in the text box in to a DateTime object before passing it to the method, but I don't know how. Any help on this would be much appreciated Thanks, Andy

Original source

Related problems