JQuery DateTime picker and ASP.NET MVC

I am currently using jQuery Date Time picker to select the date to add to the database. When using the date selection timer, the result is displayed correctly in the text field to which it is attached (IE 09/27/2009 16:00). However, the time of the date is not properly transmitted to the MVC application and is accepted as 01/01/0001 00:00:01.

This method requires one paramater - 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 length, I omitted the script, included it above, but they are present on the page. Text field and validation fields were created by visual studio.

I have a feeling that I need to somehow implicitly convert a string into a text field into a DateTime object before passing it to the method, but I don't know how to do it.

Any help on this would be greatly appreciated.

Thanks Andy

+4
source share
5 answers

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

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

Then you can use DateTime.Parse (String) to parse a string in DateTime format.

+6
source

Here is the controller action that receives data from the page:

  [ValidateInput(false)] [AcceptVerbs(HttpVerbs.Post)] public ActionResult MatchesAdd(Match m) { try { m.Against = Server.HtmlEncode(m.Against); m.Info = Server.HtmlEncode(m.Info); m.MatchID = Guid.NewGuid(); m.Played = false; m.OurScore = 0; m.EnemyScore = 0; DM.AddMatch(m); return RedirectToAction("Matches/List/Upcoming"); } catch(Exception ex) { return Content(m.TimeAndDate.ToString()); } } 

The catch block is for debugging only at this point.

As you can see, I did not understand the DateTime object, however I put a breakpoint at the beginning of the action, and the date time was incorrect from the very beginning. Can't I get the correct date?

0
source

What happens if you try this instead:

 [ValidateInput(false)] [AcceptVerbs(HttpVerbs.Post)] public ActionResult MatchesAdd(FormCollection values) { Match m = new Match(); try { UpdateModel<Match>(m); // If your date still has not been picked up you could // just uncomment this next line: // m.TimeAndDate = DateTime.Parse(values["TimeAndDate"]); DM.AddMatch(m); return RedirectToAction("Matches/List/Upcoming"); } catch(Exception ex) { return Content(m.TimeAndDate.ToString()); } } 
0
source

try it

 [ValidateInput(false)] [AcceptVerbs(HttpVerbs.Post)] public ActionResult MatchesAdd(FormCollection collection) { dateTime date = DateTime.Parse(collection["TimeAndDate"].ToString()) return view(); } 

This will give you a date.

0
source

As Andy said, I suspect this is due to the default date format that jQuery uses. With your sample code running on my PC, I get 12/22/2012 (current date, which is the American format), and I am in Australia. You can change the date format used by jQuery using dateFormat:

 <script type="text/javascript"> $(function() { $('#TimeAndDate').datepicker({ duration: '', showTime: true, constrainInput: false, dateFormat: "dd M yy" }); }); </script> 
  • jQuery: dd M yy = dd MMM yyyy (C #)
  • jQuery ddMM yy = dd MMMM yyyy (C #)

You might also want to look here: MVC DateTime binding with incorrect date format

0
source

All Articles