编辑视图中的恢复日期时间值



当我在编辑视图中单击时,datetime丢失了值。在此处输入图像描述

<div class="form-group">
@Html.LabelFor(model => model.DataNascimento, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DataNascimento, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DataNascimento, "", new { @class = "text-danger" })
</div>
</div>

当我在编辑视图中单击DataNascime的值时,我希望从数据库中接收该值。

我希望你们能帮助我。

必须强制控件显示为dd/mm/yyyy格式的日期。要做到这一点,您需要格式化日期。将模型中的日期属性装饰为以下

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime DataNascimento{ get; set; }

如果您使用的是MVC5,这应该能在中工作

@Html.EditorFor(model => model.DataNascimento, new {htmlAttributes = new {@Value = @Model.DataNascimento.ToString("dd-MM-yyyy") } })

最新更新