Why isn't my EditorFor datepicker field populating my value?

217 views Asked by At

I'm trying to get this datepicker field to populate with today's date but it's not happening for some reason. I can't find a reference for what options are accepted in the "new {}" section.

@Html.LabelFor(d => d.ServiceOn, new { @class = "control-label" })
@Html.EditorFor(d => d.ServiceOn, "DatePicker", new { disableMinDate = true, Value = DateTime.Today })
@Html.ValidationMessageFor(d => d.ServiceOn, "", new { @class = "text-danger" })

I've tried value, Value and @Value but the resulting html always shows value="". I'm wondering if maybe the datepicker itself is zeroing out the field. I feel like I'm missing something obvious here.

1

There are 1 answers

2
Jorge Zuverza On BEST ANSWER

This should work:

@Html.LabelFor(d => d.ServiceOn, new { @class = "control-label" })
@{Model.ServiceOn= DateTime.Today;}
@Html.EditorFor(d => d.ServiceOn, "DatePicker", new { disableMinDate = true })
@Html.ValidationMessageFor(d => d.ServiceOn, "", new { @class = "text-danger" })

HTML work with the ModelState, not from the model itself.

If you want more loose helpers, ise @Html.Editor instead.