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

标签 c# asp.net asp.net-mvc

在 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/

相关文章:

asp.net - 什么是 ASP.NET 中的 gpstate 文件

c# - 无法从 ASP.NET 调用 DLL

asp.net - 使用 UmbracoIdentity 和 MVC 5 身份验证的 SSO Umbraco 7

asp.net-mvc - 在 Azure 上使用 LocalDb MDF 文件

c# - 从 .NET 程序集加载项目设置

c# - 带有 TLS 的假 smtp 服务器

c# - 引用的项目依赖项是从 GAC 而不是 NuGet 包文件夹复制的

c# - SHA256 在哈希中返回无效字符

c# - 使用 ViewData 将数据从 Controller 传递到 View

asp.net-mvc - Entity Framework "Migrations Configuration"和 autofac