MVC DateTime Error

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

 <!-- language: lang-none --> @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") } 
+6
source share
3 answers

The problem was checking and localizing jQuery. It seems that there are localization files for the messages and methods of the jQuery plugin. See my blog for a detailed explanation of the problem and how I solved it.

http://www.locktar.nl/programming/mvc/localization-validation-in-mvc/

Edit: I just released a new blog post updating all localization issues and how to fix it for the DateTime property. See My new post MVC Localization Check .

+3
source

I was able to fix this by changing the jQuery validation function for dates:

  <script type="text/javascript"> $(function () { var dateFormat="dd/mm/yy"; // en-gb date format, substitute your own $("#Birthday").datepicker({ "dateFormat": dateFormat }); jQuery.validator.addMethod( 'date', function (value, element, params) { if (this.optional(element)) { return true; }; var result = false; try { $.datepicker.parseDate(dateFormat, value); result = true; } catch (err) { result = false; } return result; }, '' ); }); 

+7
source

The globalization tag is not set in web.config, so it should use it by default. Im from Holland, so he should get a customer culture, I think.

No, that is not right. From Holland you can be in perfect order and set up your browser to use Chinese culture. The reason you can't get this to work is probably because in FF and Chrome you don't have the right culture.

Since you did not specify the culture in your globalization element in web.config, ASP.NET MVC will use the one that was sent from the browser in the request header. For example, if you set up your browser for culture in the USA, the following header will be set for each request:

 Accept-Language:en-US,en;q=0.8 

Here's how it is set up in Chrome:

enter image description here

So, make sure you put the desired language first on the list.

And if you need a reliable way to use the same syntax as the one specified in your DisplayFormat attribute when binding the model, you can write your own model connecting device, as shown here: fooobar.com/questions/102628 / ...

+1
source

Source: https://habr.com/ru/post/923634/


All Articles