show only the date in @Html.EditorFor helper

后端 未结 4 1301
名媛妹妹
名媛妹妹 2020-12-04 00:33

I am trying to populate @Html.EditorFor helper. I have created a view model with the below property

[DataType(DataType.Date, ErrorMessage="D         


        
4条回答
  •  一个人的身影
    2020-12-04 01:21

    You need to use the ISO format when using type="date"

    [DataType(DataType.Date, ErrorMessage="Date only")]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? YearBought { get; set; }
    

    This will display the date in the browsers culture.

    Note there is no need to add @type = "date". The EditorFor() method will add that because of the DataType attribute. Note also that type="date" is only supported in Chrome (FireFox and IE will just generate a normal textbox)

    If you do want to display the format dd/MM/yyyy in a standard textbox then you can use

    @Html.TextBoxFor(m => m.YearBought, "{0:dd/MM/yyyy}")
    

提交回复
热议问题