Why doesn’t Data Annotation recognize Datetime?

Asked

Viewed 20 times

1

The view is not recognizing the date on formed {dd MM yyyy},ex:31/12/2020 . I’m already cracking my head for hours.

View Razor

        *[Required]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Column(TypeName = "DateTime2")]
        [Display(Name = "Data de Início")]
        public DateTime DataInicio { get; set; }*
@Html.EditorFor(m => m.DataInicio , new { @class = "data form-control" })
  • Displayformat this is the format for display, the date is treated according to the culture. To avoid this an option is to use the type string and validate the format when receiving the model in the action

  • The question that both Textboxfor and Editorfor are reading in the form MM dd yyyy. I don’t understand this. ex: 12/31/2020 OK.

  • I think you are having an error of interpretation ai... these attributes are used to render the element in the browser, displaying the date in the correct format, can render as input in the type correct (text, date, etc). When you will do the post, and it needs to be converted to the model that is received in the action, here we are talking about code .net and the date has to be converted to the format that the server will understand. If Culture is mm/dd/yyyy, it is in this format that it needs to be posted, i.e., for the post what is in the input should be parsed for the model type.

No answers

Browser other questions tagged

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