首页 > 解决方案 > 在日期选择器中显示日期时间

问题描述

在我的 mvc 应用程序中,我有一个 datepicker 控件,并且该控件不会从数据库中读取值。

模型:

 [DisplayFormat(DataFormatString = "{0:dd'.'MM'.'yyyy}", ApplyFormatInEditMode = true, ConvertEmptyStringToNull = false)]
    public DateTime? myDate { get; set; }

看法:

<div class="col-md-6" style="padding-left:0px;padding-right:0px; margin-top: 15px;">
                    @Html.LabelFor(model => model.myDate, htmlAttributes: new { @class = "control-label col-md-4" })
                    <div class="col-md-8">
                        @Html.EditorFor(model => model.myDate, new { htmlAttributes = new { @class = "form-control datepicker", @type = "date" } })
                        @Html.ValidationMessageFor(model => model.myDate , "", new { @class = "text-danger" })
                    </div>

模型中的值:model.myDate.Date {14.02.2019 0:00:00}

在它显示的视图上

dd.mm.yyyy 没有任何值。

保存工作正常。

标签: c#asp.net-mvc

解决方案


我认为问题可能出在DataFormatString.

尝试更换:

DataFormatString = "{0:dd'.'MM'.'yyyy}" 

有了这个:

DataFormatString = "{0:dd.MM.yyyy}"

推荐阅读