MVC实体框架中的日期时间字段 [英] Datetime field in MVC Entity Framework

查看:63
本文介绍了MVC实体框架中的日期时间字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have generated Controller and View using add scaffolding in Visual Studio 2017, but the datetime input field need to enter the date time manually instead of datetime picker. I have tried several ways but still not able to implement datetime picker.

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

添加时可以选择日期时间

The datetime can be selected when I added

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

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

but I need hour and minutes as well but this is not working.

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

推荐答案

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

DateTime picker can be implemented using jQuery's date time picker. Or if you want an inbuilt MVC datetime picker, modify your code as :

字段:

   [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>

这将为您呈现日期选择器.

which will then render the date picker for you.

这篇关于MVC实体框架中的日期时间字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆