简体   繁体   English

ASP.Net MVC 4 EditorFor DateTime未在Chrome中预填充

[英]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. 在Chrome浏览器中浏览时,控制器的编辑视图中的datetime输入字段不会从模型中加载当前值。 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; }
    }

Instead of using EditFor , try to use TextBoxFor . 而不是使用EditFor ,请尝试使用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")})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM