Date format in asp.net mvc [duplicate]

1

I'm having problems with the date format in en in an asp.net mvc application. Where it displays error if the date is in the dd / MM / yyyy HH: mm format, and only lets the date if it is not formed MM / dd / yyyy HH: mm .

Model

 [Display(Name = "Data de Publicação", Description = "Selecione uma data futura para agendar uma publicação")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
        public System.DateTime DataPublicacao { get; set; }

View

<div class="form-group">
            @Html.LabelFor(model => model.DataPublicacao, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-12 col-md-12" })
            <div class="col-xs-12 col-sm-12 col-md-12">
                @Html.TextBoxFor(m => m.DataPublicacao, "{0:dd/MM/yyyy HH:mm}", htmlAttributes : new { @class = "form-control date-timepicker"})
                @Html.ValidationMessageFor(model => model.DataPublicacao, "", new { @class = "text-danger" })
            </div>
        </div>

Javascript datetimepicker

$('.date-timepicker').datetimepicker({
            locale: 'pt-br',
            keepOpen: true,
            showTodayButton: true,
            format: 'DD/MM/YYYY HH:mm'
});

Webconfig :

<system.web>
    <globalization culture="pt-BR" uiCulture="pt-BR" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
  </system.web>

Result:

    
asked by anonymous 22.06.2017 / 13:44

1 answer

3

I had the same problem and what helped me to solve it was the blog post here: > MVC 3: Validation for cultures like pt-BR

In my project I'm using a newer version of ASP.NET MVC, but the problem has still been solved by adding the validation script.

As suggested by colleague David, a summary follows:

"To support the validation of dates for cultures such as pt-BR, you need to add a script with specific validation methods."

You can do this as follows: create a methods_pt.js file inside the Scripts folder of your project and paste the following content into it:

/*
 * Localized default methods for the jQuery validation plugin.
 * Locale: PT_BR
 */
jQuery.extend(jQuery.validator.methods, {
    date: function(value, element) {
        return this.optional(element) || /^\d\d?\/\d\d?\/\d\d\d?\d?$/.test(value);
    }
});

Keep in your web.config the tag <globalization culture="pt-BR" uiCulture="pt-BR" /> as you showed in your question.

Finally, on the pages where you need to use validation, include the scripts as in the following code:

<!-- Scripts de validação -->
<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>
<!-- Correção de funções de validação específicas para a cultura pt-BR -->
<script src="@Url.Content("~/Scripts/methods_pt.js")" type="text/javascript"></script>
    
22.06.2017 / 20:53