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

前端 未结 13 1174
逝去的感伤
逝去的感伤 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:19

    I couln't solve it by the others answers. In my case, using TextBoxFor wasn't mandatory; I used TextBox instead.

    Model

    [Required(ErrorMessage = "Required")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime MyDate{get;set;}
    

    View

    @Html.TextBox("MyDate", DateTime.Now.ToString(), new {@type="datetime"})
    

提交回复
热议问题