【问题标题】:How to show only date in index view in ASP.NET MVC?如何在 ASP.NET MVC 的索引视图中只显示日期?
【发布时间】:2022-11-17 18:46:26
【问题描述】:

在模型中:

public DateTime? FromDate { get;set; }

在视图中:

<div class="form-group">
    <label class="label other">
        From Date
    </label>
    <div class="col-md-10">
        @Html.EditorFor(model => model.FromDate, new { htmlAttributes = new { @class = "form-control", type = "Date", min = "1944-01-01", max = "2019-01-01" } })
        @Html.ValidationMessageFor(model => model.FromDate, "", new { @class = "text-danger" })
    </div>
</div>

当我们从 DateTime 选择器日历中输入日期时,它会输入日期:

Index 视图中它还显示了时间:

问题是我不想在索引视图中显示时间和日期。

【问题讨论】:

    标签: c# asp.net-mvc razor


    【解决方案1】:

    要为 DateTime 属性类型设置显示格式,请在模型声明中使用 DisplayFormat 属性,如下所示:

    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? FromDate { get; set; }    
    

    【讨论】:

    • 感谢我的问题解决了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 2020-06-14
    • 2018-11-16
    • 1970-01-01
    相关资源
    最近更新 更多