Monday 20 July 2015

Allow Datetime null in scaffolding model

When you are trying to do scaffolding with a model which has datetime filed , the common code is as follows.
    
        [Display(Name = "End Date")]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime EndDate { get; set; }


But this makes this filed mandatory by default. So in-order to make it nullable , we need to alter the code as following.
    
        [Display(Name = "End Date")]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
        Public Nullable<DateTime>  EndDate { get; set; }


No comments:

Post a Comment