在特定于 ASP .NET MVC 5.1/HTML 5 的 ASP .NET MVC 5.1/HTML 5 中显示日期时间选择器而不是日期选择器 [英] Displaying DateTime picker instead of Date picker in ASP .NET MVC 5.1/HTML 5 specific

查看:23
本文介绍了在特定于 ASP .NET MVC 5.1/HTML 5 的 ASP .NET MVC 5.1/HTML 5 中显示日期时间选择器而不是日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ASP .NET MVC 5.1 中编写应用程序

我有一个字段:

 [DisplayName("出生日期")][数据类型(数据类型.日期)]公共日期时间出生日期 { 获取;放;}

然后在视图中

 

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

翻译成:

如果我只是将模型中属性上方的注释更改为:

 [DisplayName("出生日期")][数据类型(DataType.DateTime)]

我没有显示任何选择器.如果我将它们更改为:

 [DisplayName("出生日期")][数据类型(数据类型.时间)]

只有 hh:mm 可供选择.

问题: 在 ASP .NET MVC 5.1 中显示日期时间选择器而不是日期选择器.我要求 ASP .NET 5.1 HTML 5 特定解决方案.

解决方案

你的要求很挑剔...

为字段指定属性Datatype,将生成具有指定属性的input 作为type.这就是为什么当您添加 [DataType(DataType.Date)] 时,生成的输入将是一个日期选择器,但是如果您添加 [DataType(DataType.DateTime)],输入将是 datetime 类型,因此您没有显示任何选择器.为什么?因为几年前,支持输入 datetime 的浏览器决定停止支持它,W3C 由于缺乏实现删除了这个功能.

然而,不久前,一些浏览器供应商再次开始支持 datetime-local,这又回到了草案中.目前,最新版本的 ChromeOperaMicrosoft Edge 都支持它.

一种解决方案是使用 Ajay Kumar 建议的 BootStrap 或 jQuery Datetime Picker.

如果您不介意少数支持的浏览器,另一种方法是使用 datetime-local.例如像这样:

@Html.TextBoxFor(model => model.BirthDate, new { type = "datetime-local"})

I write application in ASP .NET MVC 5.1

I have a field:

  [DisplayName("Date of Birth")]
  [DataType(DataType.Date)]
  public DateTime BirthDate { get; set; }

and then in View

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

which translates to:

if I just change annotiations above property in model to:

    [DisplayName("Date of Birth")]
    [DataType(DataType.DateTime)]

I don't get any picker displayed. If I change them to:

 [DisplayName("Date of Birth")]
 [DataType(DataType.Time)]

there is only hh:mm to choose from.

Question: Displaying DateTime picker instead of Date picker in ASP .NET MVC 5.1. I am asking for ASP .NET 5.1 HTML 5 specific solution.

解决方案

Your requirements are pretty picky...

Specifying the attribute Datatype for a field, will generate an input having as type the attribute specified. That's why when you add [DataType(DataType.Date)], the input generated will be a date picker, but if you add [DataType(DataType.DateTime)], the input will be of type datetime, thus why you don't get any picker displayed. Why? Because a few years ago, the browsers supporting input datetime decided to stop supporting it and W3C removed this feature due to lack of implementation.

However, not so long ago, some browser vendors started supporting again datetime-local which is back on the draft. As of now, the latest versions of Chrome, Opera and Microsoft Edge support it.

One solution would be to use the BootStrap or the jQuery Datetime Picker as Ajay Kumar suggested.

Another one would be, if you don't mind the few supporting browsers, to use datetime-local. Like this for instance:

@Html.TextBoxFor(model => model.BirthDate, new { type = "datetime-local"})

这篇关于在特定于 ASP .NET MVC 5.1/HTML 5 的 ASP .NET MVC 5.1/HTML 5 中显示日期时间选择器而不是日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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