user2886826
user2886826

Reputation: 47

ASP.Net MVC 4 EditorFor DateTime not prepopulating in Chrome

A datetime input field in the edit view of a controller isn't loading the current value from the model when browsed in Chrome. The page loads with a time editor field, but the value is empty. The intent is to display the time editor, and have it be prepopulated with the current value on page load. I've searched around, and most of the fixes involve applying a format string, but this doesn't work for me. I'd like some help on getting this to work. Thanks.

View

<div class="editor-label">
    @Html.LabelFor(model => model.BeginTime)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.BeginTime)
    @Html.ValidationMessageFor(model => model.BeginTime)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.EndTime)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.EndTime)
    @Html.ValidationMessageFor(model => model.EndTime)
</div>  

Model

public class WorkInterval
    {
        public int WorkIntervalID { get; set; }

        [Required]
        [DataType(DataType.Time)]
        public DateTime BeginTime { get; set; }

        [Required]
        [DataType(DataType.Time)]
        public DateTime EndTime { get; set; }
    }

Upvotes: 2

Views: 3482

Answers (1)

Lin
Lin

Reputation: 15188

Instead of using EditFor, try to use TextBoxFor. See below code:

@Html.TextBoxFor(model => model.BeginTime,new{@id="Datepicker",@Value = Model.BeginTime.ToString("MM/dd/yyyy")})

@Html.TextBoxFor(model => model.EndTime,new{@id="Datepicker",@Value = Model.EndTime.ToString("MM/dd/yyyy")})

Upvotes: 1

Related Questions