我有以下代码来格式化我的观看日期。
[DisplayName("Date Last Seen")]
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? LostDate { get; set; }
这一切都有效,只有当我点击编辑视图时," LostDate"的当前值。不会自动呈现。
我该如何解决这个问题?
修改 我在编辑视图中有以下代码。
<div class="form-group">
@Html.LabelFor(model => model.LostDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LostDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LostDate, "", new { @class = "text-danger" })
</div>
</div>
编辑2 我已经通过更改&#34; {0:dd-MM-yyyy}&#34;解决了未出现日期的问题。到{0:yyyy-MM-dd},但在我的所有其他视图中,日期现在以第一年格式显示。
答案 0 :(得分:0)
创建类构造函数并设置默认值。例如。
public Classname()
{
LostDate=DateTime.Now;
}
public DateTime? LostDate { get; set; }
其他选项
使用自定义验证。 so refer this link in more details
试一试。