在我的INV_Assets
模型中,我定义了以下日期字段:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? acquired_date { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? disposed_date { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime created_date { get; set; }
然后在我Edit()
看来,我form-group
为这些 Date 值中的每一个定义了以下内容:
<div class="form-group">
@*@Html.LabelFor(model => model.acquired_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Acquired Date:</span>
<div class="col-md-10">
@Html.EditorFor(model => model.acquired_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.acquired_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.disposed_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Disposed Date:</span>
<div class="col-md-10">
@Html.EditorFor(model => model.disposed_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.disposed_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.created_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Created Date:</span>
<div class="col-md-10">
@Html.EditorFor(model => model.created_date, new { htmlAttributes = new { @class = "form-control", @Value = Model.created_date.ToString("yyyy-MM-dd") } })
@Html.ValidationMessageFor(model => model.created_date, "", new { @class = "text-danger" })
</div>
</div>
现在,当created_date
有一个值时,我可以打开表单并EditorFor
正确显示该值。但是,即使在数据库中保存一个值,也EditorFor()
可以始终出现,即使在编辑器中保存了一个值,而编辑器仍然显示数据库中的值更改为。acquired_date
disposed_date
mm/dd/yyyy
mm/dd/yyyy
null
当我尝试为 an指定acquired_date
and disposed_date
with (Ex.)时,两者都标记为...?@Value = Model.disposed_date.ToString("yyyy-MM-dd")
htmlAttribute
Model.acquired_date.ToString("yyyy-MM-dd")
Model.disposed_date.ToString("yyyy-MM-dd")
No overload method for 'ToString' takes 1 argument
谁能看到我做错了什么?
理想情况下,我想将输入的值与Date
用户在保存时指定的值EditorFor
和当前值一起Time
保存。mm/dd/yyyy
然后,EditorFor
如果用户正在查看记录,我想以格式显示值。