Display datetime model property as Short date time string

前端 未结 1 1667
感情败类
感情败类 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:

    <div class="editor-field">
        <%: Html.EditorFor(model => model.DateRequested) %>
        <%: Html.ValidationMessageFor(model => model.DateRequested) %>
    </div>
    

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

    <div class="editor-field">
        <%: Html.TextBox("DateRequested", Model.DateRequested.ToShortDateString()) %>
        <%: Html.ValidationMessageFor(model => model.DateRequested) %>
    </div>
    
    0 讨论(0)
提交回复
热议问题