Date field giving required error on validation

后端 未结 1 349
梦如初夏
梦如初夏 2021-01-11 22:53

I have created a model in my asp.net MVC 3 website and have a property named DateOpened:

  [Column(\"Date Opened\")]
        [Display(Name = \"Date Opened:\"         


        
1条回答
  •  生来不讨喜
    2021-01-11 23:10

    That's normal. DateTime is a value type meaning that it will always require a value. The model metadata provider in ASP.NET MVC automatically adds the required attribute to non-nullable data types. You could use a nullable DateTime:

    [Column("Date Opened")]
    [Display(Name = "Date Opened:")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime? DateOpened { get; set; }
    

    0 讨论(0)
提交回复
热议问题