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:
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":
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:
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: 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 of the "Message Date field".
Does anyone know why this problem is happening and how I can solve it? Thanks in advance.
Post source code by text and not by image. It’s easier to understand and help.
– Don't Panic
Try using Dataannotations to define its format.
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMMM/yyyy}")]
– Diego Vieira
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."
– Joao Pedro
Which Data Annotations you used?
– Cassio Alves
you either leave the 2 ways or just 26/06/2017 ?
– Dorathoto