Problems with Data and Globalize

Asked

Viewed 1,235 times

1

When performing the Submit in a form with a date dd/MM/yyyy, the value is converted to the American format in the controller, leaving MM/dd/yyyy. Through the answer of this question, configure globalize to solve this problem. However, the error persists.

Follow below Configured Bundle:

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate.js",
                    "~/Scripts/jquery.validate.globalize.js",
                    "~/Scripts/jquery.validate.unobtrusive.min.js",
                    "~/Scripts/jquery.unobtrusive-ajax.js"));

        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/globalize").Include(
                  "~/Scripts/globalize.js"));

Code to load the Bundles

@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/globalize")
@Scripts.Render("~/bundles/jqueryval")

Configuration line in section System.Web in the Web.Config

<globalization uiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true" requestEncoding="UTF-8" responseEncoding="UTF-8" fileEncoding="UTF-8" />

Field datetime in cshtml

@Html.TextBoxFor(model => model.Inicio, "{0:dd/MM/yyyy}", new { @class = "form-control" })

3 answers

1

I believe my answer pointed out by you is outdated. This one should more specifically solve your problem. For the specific case of jQuery Validate and Globalize, you need to have a specific sort of scripts.

Avoid separating the Bundle of the Globalize of Bundle jQuery Validate. Also avoid using @Html.EditorFor() for date fields. Use @Html.TextBoxFor() in place.


EDIT

Use of the @Html.TextBoxFor() for the case of dates:

@Html.TextBoxFor(model => model.MinhaData, "{0:dd/MM/yyyy}", new { @class = "minha_classe_de_css"})
  • Gypsy, I’ve been using Editorfor because it worked perfectly on Chrome, but the same didn’t happen on Firefox or IE. With Textbox, it was not working in any browser. I will check your link

  • I performed the implementation of the topic you posted, but the error persists. In the Javascript log no error appears, previously appeared something related to Globalize.Parse.

  • @Vinícius Now it can be the internal validation. See the edition I just put.

  • I did as you indicated, but keep changing the dates in the controller. I updated the question with the Razor field in cshtml.

  • Observing the value of the Request["Start"] key, I see that the value comes correctly from html but it is not converted correctly to datetime.

  • You can ask your question how Globalize was installed?

Show 1 more comment

0

I believe this error is related to the culture configured in the jQuery validation.

Try adding the script Scripts/cultures/Globalize.culture.pt-BR.js to Bundle and then add this script to override jQuery date validation:

<script type="text/javascript">
    $(function () {
        $.validator.methods.date = function (value, element) {
            Globalize.culture("pt-BR");
            return this.optional(element) || Globalize.parseDate(value) !== null;
        }
    });
</script>

Information found here.

  • The Globalize.culture.en-BR.js script was not installed together with Globalize, nor Globalize.Validation.

  • Executed the Install-Package jquery.Validation.Globalize in the Package Manager Console?

  • 1

    Yes... he installed Jquery.Validation.Globalize.js but did not add the Culture folder.

  • @Vinicius has the same problem, you managed to solve ?

  • 1

    @Matheusmiranda I will publish the reply

  • Hello @Vinícius, I made a small tutorial here: https://answall.com/a/227756/54019

Show 1 more comment

0

Vinicius I know it’s complicated, I made a small tutorial here:

/a/227756/54019

When you execute the command Install-Package jquery.Validation.Globalize, does not come complete package. You must do this manually.

On the website globalize documented:

We do NOT embed any i18n data Within our library. However, we make it really easy to use. Read How to get and load CLDR JSON data for more information on its Usage.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.