I had a very difficult time to solve this problem (and I'm sure I'm making some kind of stupid mistake somewhere :)) Can someone please help.
I cannot get the date selected using jQuery datepicker in the model when sending. This gives me the value "1/1/0001 12:00:00 AM". Did I miss something?
To isolate my problem, I created a simple MVC3 application using a template from Visual Studio 2010. Details:
Model:
namespace DatePickerApp.Models { public class DatePickerClass { public DateTime BirthDate { get; set; } } }
Controller:
namespace DatePickerApp.Controllers { public class BirthDateController : Controller { // // GET: /BirthDate/Create public ActionResult Create() { return View(); } // // POST: /BirthDate/Create [HttpPost] public ActionResult Create(DatePickerApp.Models.DatePickerClass DatePickerClass) { try { // TODO: Add insert logic here return View(); } catch { return View(); } } } }
cshtml View:
@model DatePickerApp.Models.DatePickerClass @{ ViewBag.Title = "Create"; } <h2>Create</h2> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" /> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script> <link rel="stylesheet" href="/resources/demos/style.css" /> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $("#date").datepicker({ dateFormat: 'dd/mm/yy' }); }); </script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>DatePickerClass</legend> <div class="editor-label"> @Html.LabelFor(model => model.BirthDate) </div> <div class="editor-field"> @Html.TextBoxFor(model => model.BirthDate, new { id = "date" }) @Html.ValidationMessageFor(model => model.BirthDate) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>
source share