在 Chrome 中浏览时, Controller 编辑 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>
型号
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; }
}
最佳答案
尝试使用 TextBoxFor
而不是使用 EditFor
。见以下代码:
@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")})
关于c# - ASP.Net MVC 4 EditorFor DateTime 未在 Chrome 中预填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20329146/