Display datetime model property as Short date time string

前端 未结 1 1666
感情败类
感情败类 2021-02-15 20:02

I\'m new with MVC2, and am having a formatting problem. I have a DateTime property in my Employee model that I would like displayed with the Short Date Time.

This howeve

1条回答
  •  囚心锁ツ
    2021-02-15 20:38

    Try decorating your view model property with the [DisplayFormat] attribute:

    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime DateRequested { get; set; };
    

    and in your view use the Html.EditorFor helper:

    <%: Html.EditorFor(model => model.DateRequested) %> <%: Html.ValidationMessageFor(model => model.DateRequested) %>

    or if you insist on using textbox helper (don't know why would you but anyway here's how):

    <%: Html.TextBox("DateRequested", Model.DateRequested.ToShortDateString()) %> <%: Html.ValidationMessageFor(model => model.DateRequested) %>

    0 讨论(0)
提交回复
热议问题