在EditorFor中显示[Date]值:'ToString'没有重载方法需要1个参数?

时间:2015-02-17 21:05:01

标签: asp.net-mvc datetime razor editorformodel

在我的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

<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的{​​{1}}始终显示为disposed_date,即使数据库中保存了值 - 如果表单已保存在编辑器仍然显示mm/dd/yyyy数据库更改为mm/dd/yyyy的值。

当我尝试为null指定acquired_datedisposed_date与{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格式显示该值。

2 个答案:

答案 0 :(得分:4)

你有一些事情要发生在这里。首先,我假设EditorFor正在生成类型为date的输入。对于HTML5 date输入类型,提供的值必须为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)

答案 1 :(得分:1)

格式必须为[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)](ISO格式)