jQuery DateTime picker and ASP.NET MVC

15,659

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.

Share:
15,659
Admin
Author by

Admin

Updated on August 21, 2022

Comments

  • Admin
    Admin almost 2 years

    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

  • Admin
    Admin almost 15 years
    I'm not passing the date time to the controller as a parameter, I'm passing it as part of an object: public ActionResult MatchesAdd(Match m
  • Mickel
    Mickel almost 15 years
    That's probobly why it's not getting recognized. It's hard for us to see what you have done wrong if you just put up this small part of your code.
  • Admin
    Admin almost 15 years
    If you need to see any more parts, just ask. Otherwise, I've put up everything I can think of that's relevant. The Match object contains the field TimeAndDate as a DateTime, and is derived from an SQL table named Matches.
  • Mickel
    Mickel almost 15 years
    Does the Match object contain any other fields? If so: can you verify that these are being received correctly in your controller?
  • Admin
    Admin almost 15 years
    Yes, the other fields are being recieved correctly. They are: MatchID(Guid), Info(string), TeamID(Guid), Against(string), Played(bit\bool), OurScore(int), EnemyScore(int), TimeAndDate(DateTime)
  • Admin
    Admin almost 15 years
    Unfortunately, that brings no luck. Still catches an exception