Sending shortened date does not work abbreviated Asp.net mvc

Asked

Viewed 199 times

2

I am creating a form in ASP.NET MVC and, creating the field "Datamessage", I initially put Textbox to load a date with the month in full, as below:

Imagem do formulário com o campo "Data da mensagem" contendo o mês por extenso.

Image of the form with the field "Date of the message" containing the month in full.

Below, the View code of the "Message Date field":

Imagem do código utilizado para criar o campo "Data da Mensagem" com o mês por extenso Screenshot of the View code used to create the "Message Date" field with the month spelled out.

When I create the field this way, in addition to loading correctly, I can send the data to the database without problems in case of a possible change.

However, when I use the format to abbreviate the Month and show only its number, the validation does not allow me to send it to the database in case of a possible message change.

Below, the form with the "Message Date" field loaded correctly with the abbreviated month:

Imagem do formulário com o campo "Data da mensagem" contendo o mês abreviado, com o erro apresentado ao clicar no botão "Salvar".

Image of the form with the "Message Date" field containing the abbreviated month, with the error shown when clicking the "Save" button".

Below, the View code of the "Message Date" field with the abbreviated month:Imagem do código da View utilizado para criar o campo "Data da Mensagem" com o mês abreviado. Screenshot of the View code used to create the "Message Date" field with the abbreviated month.

And below, the model used for the field "Message Date":

Model do campo "Data da Mensagem".

Model of the "Message Date field".

Does anyone know why this problem is happening and how I can solve it? Thanks in advance.

  • 1

    Post source code by text and not by image. It’s easier to understand and help.

  • Try using Dataannotations to define its format. [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMMM/yyyy}")]

  • I’ve tried using Dataannotations, and it didn’t work. When I use it, the date and full hours appear ("26/06/2017 12:00:00" for example), and when I try to send the data, returns the error "The Data field of the message must be a date."

  • Which Data Annotations you used?

  • you either leave the 2 ways or just 26/06/2017 ?

1 answer

1


Your problem is related to the browser that is understanding that the first field of the date is the month, you can test any value over 12 that will generate the same problem because it tries to parse one for month and understand that the value is invalid.

You may notice that if you put 01/02/2017, Asp.net will receive the day 01 and the month 02 correctly, confirming that the validation problem is only client-side.

I found a snippet (here) a while ago when I had this problem that can help you (assuming you are using the Jquery validation that comes in MVC by default) and I just created a console application to test and solved:

<script src="~/Scripts/jquery.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

    $(function () {
        $.validator.methods.date = function (value, element) {
            if ($.browser.webkit) {

                //ES - Chrome does not use the locale when new Date objects instantiated:
                var d = new Date();

                return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
            }
            else {

                return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
            }
        };
    });
</script>

Browser other questions tagged

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