在我的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没有任何值。
虽然保存效果很好。
答案 0 :(得分:0)
我的同事通过以下方式编辑“视图”解决了该问题:
<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 { @Value = (@Model.myDate.HasValue ? @Model.myDate.Value.ToString("yyyy-MM-dd") : null), @class = "form-control datepicker", @type = "date" } })
@Html.ValidationMessageFor(model => model.myDate, "", new { @class = "text-danger" })
</div>
</div>
答案 1 :(得分:0)
我认为问题可能出在DataFormatString
。
尝试更换:
DataFormatString = "{0:dd'.'MM'.'yyyy}"
与此:
DataFormatString = "{0:dd.MM.yyyy}"