2

I have an Edit view. A filled in ViewModel is passed in which the user can edit and submit.

    <div class="form-group">
        <strong>Date Effective:</strong>
        <div class="col-md-10">
            <p>
                @Html.EditorFor(model => model.storeConfiguration.DateEffective)
                @Html.ValidationMessageFor(model => model.storeConfiguration.DateEffective)
        </div>
    </div>

The view model contains:

public DateTime DateEffective { get; set; }

This showed the date as a string and allowed easy edits. However, I came across data annotations and found that by adding [DataType(DataType.Date)] I was able to get a useful tool to pick a date.

Unfortunately when used in my edit view above, it shows an empty date picker rather than displaying the date that is in the ViewModel.

Is it possible to get this date picking tool without erasing the ViewModel data from my edit view?

EDIT:

While attempting to track down this bug, I came across this while looking at the 'Inspect Element'

<input class="text-box single-line" data-val="true" data-val-date="The field DateEffective must be a date." id="storeConfiguration_DateEffective" name="storeConfiguration.DateEffective" type="date" value="11/5/2014">

This shows the correct value is still stored there, it's just not being displayed to the user.

EDIT 2:

Preliminary testing has shown that [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")] is working. I don't fully understand it but it seems that that is the format the date needs to be in for the view to access it. From there, the EditorFor is able to display it in the date picking tool which is organized MM/dd/yyyy which is fine for me.

If anyone has a solid answer about what this is doing and why, I'll accept it as the chosen answer.

NerdyFool
  • 531
  • 1
  • 7
  • 14

1 Answers1

0

Based on this SO the issue may be the date format in which the value property is being set.

Community
  • 1
  • 1
mreyeros
  • 4,359
  • 20
  • 24
  • This is what the error message I just found is giving. I think you might be on the right track. `The specified value '11/05/2014' does not conform to the required format, 'yyyy-MM-dd'.` – NerdyFool Jan 06 '15 at 22:09