Assign format of DateTime with data annotations?

前端 未结 10 1548
梦如初夏
梦如初夏 2020-11-27 14:03

I have this attribute in my view model:

[DataType(DataType.DateTime)]
public DateTime? StartDate { get; set; }

If I want to display the dat

10条回答
  •  庸人自扰
    2020-11-27 14:11

    set your property using below code at the time of model creation i think your problem will be solved..and the time are not appear in database.you dont need to add any annotation.

    private DateTime? dob;
            public DateTime? DOB
            {
                get
                {
                    if (dob != null)
                    {
                        return dob.Value.Date;
                    }
                    else
                    {
                        return null;
                    }
    
                }
                set { dob = value; }
            }
    

提交回复
热议问题