Assign format of DateTime with data annotations?

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

    Use this, but it's a complete solution:

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    
    0 讨论(0)
  • 2020-11-27 14:24

    In mvc 4 you can easily do it like following using TextBoxFor..

    @Html.TextBoxFor(m => m.StartDate, "{0:MM/dd/yyyy}", new { @class = "form-control default-date-picker" })
    

    So, you don't need to use any data annotation in model or view model class

    0 讨论(0)
  • 2020-11-27 14:26

    Use EditorFor rather than TextBoxFor

    0 讨论(0)
  • 2020-11-27 14:35

    That works for me

    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    
    0 讨论(0)
提交回复
热议问题