1

在我的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_datedisposed_datemm/dd/yyyymm/dd/yyyynull

当我尝试为 an指定acquired_dateand disposed_datewith (Ex.)时,两者都标记为...?@Value = Model.disposed_date.ToString("yyyy-MM-dd")htmlAttributeModel.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如果用户正在查看记录,我想以格式显示值。

4

2 回答 2

4

你有一些事情要做。首先,我假设它EditorFor正在生成一个类型为date. 对于 HTML5date输入类型,提供的值必须采用 ISO 格式 (YYYY-MM-DD)。如果它不是这种格式,那么它将被视为您没有提供任何值。在发布后,它会被保存到您的数据库中,从而产生空值。总而言之,您需要将显示格式更改为:

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyInEditMode = true)]

这是视图模型派上用场的地方,因为您可以在用于编辑对象的类上设置此设置,并将您真正想要的显示格式设置在用于查看对象的类上。

其次,您的日期属性是可为空的。所以你不能ToString直接用他们的格式打电话。相反,您必须执行以下操作:

@Model.disposed_date.Value.ToString("MM/dd/yyyy")

但是,如果值为 null,那将失败,因此您始终需要通过将其包装在 if 块中来确保您首先拥有一个值:

@if (Model.disposed_date.HasValue)
{
    ...
}

或使用三元:

@(Model.disposed_date.HasValue ? Model.disposed_date.Value.ToString("MM/dd/yyyy") : string.Empty)
于 2015-02-17T21:31:39.950 回答
1

格式需要是[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)](ISO格式)

于 2015-02-17T21:32:18.517 回答