问题描述
我正在使用 Visual Studio 2015,并且在我的 MVC5 应用程序中使用它的默认日期时间选择器.我在显示日期时间选择器时遇到问题.它只显示日期选择器而不是时间选择器.但我也需要时间选择器.这是我在模型中的 datetimepicker 代码
I am using visual studiio 2015 and i am using its default datetime picker in my MVC5's App. i am facing problem while showing the datetime picker. it is showing only date picker and not time picker. but i need time picker also. here is my code of datetimepicker in model
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = false)]
[Required(ErrorMessage = "Start DateTime is required")]
public Nullable<System.DateTime> start_date_time { get; set; }
当我设置 DateTime
代替 Date
没有控制器时,只出现文本框.
while i set DateTime
in place of Date
No Controller comes only textbox appear.
推荐答案
MVC5 中没有默认的日期时间选择器.[DataType]
属性的作用是为您生成的 <input>
元素渲染 type
属性,然后浏览器将使用该元素如果支持,则呈现适当的 HTML5 控件.在DataType.Date
的情况下,生成
There is no default datetime picker in MVC5. What the [DataType]
attribute does is render the type
attribute for the <input>
element your generating that will then be used by the browser to render the appropriate HTML5 control if supported. In the case of DataType.Date
, it generates <input type="date" ... />
目前只有最新版本的 Chrome 支持 type="date"
.如果您使用 IE 或 FireFox 对此进行测试,您只会得到一个标准的文本框.
At the current time only recent versions of Chrome support type="date"
. If you tested this using IE or FireFox, you would only get a standard textbox.
使用 [DataType(DataType.DateTime)]
生成 type="datetime"
但目前 Chrome、IE 或 FireFox 都不支持,这就是为什么你只看到标准文本框.
Using [DataType(DataType.DateTime)]
generates type="datetime"
but this is not currently supported in either Chrome, IE or FireFox, which is why you see only the standard textbox.
不过,最新版本的 Chrome 确实支持 type="datetime-local"
,它将呈现一个控件,允许您选择日期和时间.没有生成它的 [DataType]
类型,但您可以使用
Recent versions of Chrome however do support type="datetime-local"
which will render a control allowing you to select both a date and a time. There is no [DataType]
type which generates this but you can use
@Html.TextBoxFor(m => m.start_date_time, "{0:s}", new { @type = "datetime-local" })
注意 "{0:s"}
是 "{0:yyyy-MM-ddTHH:mm:ss}"
您可以使用 本网站.
由于目前支持有限,我建议您考虑使用 jquery datetimepicker 插件而不是浏览器的 HTML 实现.
Due to the current limited support, I recommend you consider using a jquery datetimepicker plugin rather than browsers HTML implementations.
这篇关于Visual Studio 2015 中的默认日期时间选择器仅显示不允许选择时间的日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!