【问题标题】:Datetime field in MVC Entity FrameworkMVC 实体框架中的日期时间字段
【发布时间】:2018-04-11 04:33:31
【问题描述】:

我已经在 Visual Studio 2017 中使用添加脚手架生成了控制器和视图,但是日期时间输入字段需要手动输入日期时间而不是日期时间选择器。我尝试了几种方法,但仍然无法实现日期时间选择器。

    [DataType(DataType.DateTime)]
    public DateTime EnrollmentDate { get; set; }        

添加的时候可以选择日期时间

    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

但我也需要小时和分钟,但这不起作用。

    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd-hh-mm-tt}", ApplyFormatInEditMode = true)]

【问题讨论】:

标签: c# asp.net-mvc entity-framework


【解决方案1】:

日期时间选择器可以使用 jQuery 的日期时间选择器来实现。或者,如果您想要一个内置的 MVC 日期时间选择器,请将您的代码修改为:

字段:

   [DataType(DataType.Date)]
   public DateTime EnrollmentDate { get; set; }  

然后在视野中

  <div class="form-group">
            @Html.LabelFor(model => model.EnrollmentDate , htmlAttributes: new {@class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EnrollmentDate , new {htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EnrollmentDate , "", new { @class = "text-danger" })
            </div>
        </div>

然后它将为您呈现日期选择器。

【讨论】:

  • 我试图选择日期和时间,但我认为 jQuery 没有日期时间选择器,所以我只使用日期选择器
【解决方案2】:

我相信你要找的是这个

[DisplayFormat(DataFormatString = "{0:yyyy-MM-ddThh:mm}", ApplyFormatInEditMode = true)]

注意大写T

于 20/01/2020 下午 12:04 制作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    相关资源
    最近更新 更多