locked
DataAnnotations, ApplicationDbContext and generate null DateTime RRS feed

  • Question

  • User-1422983141 posted

    In my Model:

     [DataType(DataType.DateTime)]
     [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
     [Display(Name = "Expiry Date", Description = "Expiry Date if Applicable.", Prompt = "Expiry Date", ShortName = "Expiry", Order = 1)]
     public DateTime ExpiryDate { get; set; }

    ApplicationDbContext creates the Table Definition for me and the field for ExpiryDate is created and set too, Not Null.

    ExpiryDate (datetime2(7),not null)

    I would like the table field to be set to null as this value may or may not be relevant to a entry.

    I would like to change the model annatations and have it flow through?

    Monday, September 9, 2019 12:14 AM

Answers

  • User1034446946 posted
    public DateTime? ExpiryDate { get; set; }

    use the ? to allow it to be null, and the input box will be empty by default, if you need to make sure there is a value use [Required] as well

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 9, 2019 1:04 AM