Analytic Lunatic
Analytic Lunatic

Reputation: 3934

Displaying [Date] Value in EditorFor: No overload method for 'ToString' takes 1 argument?

In my INV_Assets Model I have the following Date fields defined:

[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; }

Then on my Edit() view I have the following form-group's defined for each of these Date values:

<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>

Now then, when created_date has a value, I can open the form and the EditorFor will properly display the value. However, the EditorFor()'s for acquired_date and disposed_date always show up as mm/dd/yyyy even if there is a value saved in the Database -- if the form is saved while the editor still shows mm/dd/yyyy the value in database changes to null.

When I try to specify acquired_date and disposed_date with (Ex.) @Value = Model.disposed_date.ToString("yyyy-MM-dd") for an htmlAttribute, both Model.acquired_date.ToString("yyyy-MM-dd") and Model.disposed_date.ToString("yyyy-MM-dd") flag as No overload method for 'ToString' takes 1 argument...?

Can anyone see what I am doing wrong?

Ideally I want to save the entered values with whatever Date the user specifies in the EditorFor and with the current Time at the moment of save. I then would like to display the value in format mm/dd/yyyy within the EditorFor if the user is viewing the record.

Upvotes: 1

Views: 1052

Answers (2)

user3559349
user3559349

Reputation:

The format needs to be [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] (ISO format)

Upvotes: 1

Chris Pratt
Chris Pratt

Reputation: 239220

You've got a few things going here. First, I'd assume that EditorFor is generating an input with type of date. For the HTML5 date input type, the provided value must be in ISO format (YYYY-MM-DD). If it's not in this format, then it's treated as if you provided no value. On post, this is then saved to your database, resulting in the null values. Long and short, you need to change your display format to:

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

This is where view models come in handy, as you can have this set on the a class used for editing the object and your true desired display format set on a class used to view the object.

Second, your date properties are nullables. So you can't call ToString with a format directly off them. Instead, you have to do something like:

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

However, that will fail if the value is null, so you always need to make sure you have a value first by either wrapping it in an if block:

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

Or using a ternary:

@(Model.disposed_date.HasValue ? Model.disposed_date.Value.ToString("MM/dd/yyyy") : string.Empty)

Upvotes: 4

Related Questions