how to fix 'The field must be a date' on a datetime property in mvc

前端 未结 13 1202
逝去的感伤
逝去的感伤 2020-12-29 06:59

I need to capture date and time both for my model property. In my model class I have the following

[Required]
[DataType(DataType.DateTime)]
public DateTime?          


        
13条回答
  •  情书的邮戳
    2020-12-29 07:08

    In the model for example:

        [Display(Name = "Insert Start Date")]
        [Required(ErrorMessage = "You must specify the date of the event!")]
        [DataType(DataType.DateTime, ErrorMessage = "Podaj prawidłową datę wydarzenia")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm}", ApplyFormatInEditMode = true)]
        public DateTime StartDate { get; set; }
    
        [Display(Name = "Insert End Date")]
        [Required(ErrorMessage = "You must specify the date of the event!")]
        [DataType(DataType.DateTime, ErrorMessage = "Podaj prawidłową datę wydarzenia")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm}", ApplyFormatInEditMode = true)]
        public DateTime EndDate { get; set; }
    

    And View my example code:

    
    
                                     
                  
提交回复
热议问题