Assign format of DateTime with data annotations?

前端 未结 10 1557
梦如初夏
梦如初夏 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:15

    If your data field is already a DateTime datatype, you don't need to use [DataType(DataType.Date)] for the annotation; just use:

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    

    on the jQuery, use datepicker for you calendar

        $(document).ready(function () {
            $('#StartDate').datepicker();
        });
    

    on your HTML, use EditorFor helper:

        @Html.EditorFor(model => model.StartDate)
    

提交回复
热议问题