I found many questions for simulair, but not a good clean solution that works. I see a lot of custom code to make this work, but why? If this does not work from the very beginning?
I find it strange that it works in IE9, but it fails in Firefox and Chrome. Every time I try in Firefox or Chrome, I get the message "Birthday field must be a date."
When I try to execute the code below in a new RTM MVC 4 project, I cannot get it to work. I see the default DateTime.Now value as dd-MM-yyyy (Holland) in all browsers, but I cannot send it to Firefox and Chrome.
The globalization tag is not set in web.config, so it should use the default value. Im from Holland, so he should get a customer culture, I think.
public class RegisterModel { [Required] [Display(Name = "User name")] public string UserName { get; set; } [Required] [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] //[DataType(DataType.Date)] public DateTime Birthday { get; set; } } [AllowAnonymous] public ActionResult Register() { RegisterModel vm = new RegisterModel() { Birthday = DateTime.Now }; return View(vm); } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { //WebSecurity.CreateUserAndAccount(model.UserName, model.Password); //WebSecurity.Login(model.UserName, model.Password); return RedirectToAction("Index", "Home"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return View(model); }
Markup
@model DateTimeWithDatePicker.Models.RegisterModel @{ ViewBag.Title = "Register"; } <hgroup class="title"> <h1>@ViewBag.Title.</h1> <h2>Create a new account.</h2> </hgroup> @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary() <fieldset> <legend>Registration Form</legend> <ol> <li> @Html.LabelFor(m => m.UserName) @Html.TextBoxFor(m => m.UserName) </li> <li> @Html.LabelFor(m => m.Birthday) @Html.EditorFor(m => m.Birthday) </li> </ol> <input type="submit" value="Register" /> </fieldset> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
source share