Default datetime picker in visual studio 2015 showing only date picker not allowing to select time

橙三吉。 提交于 2019-12-02 05:51:14

问题


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; }

while i set DateTime in place of Date No Controller comes only textbox appear.


回答1:


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" ... />

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.

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.

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" })

Note "{0:s"} is shorthand for "{0:yyyy-MM-ddTHH:mm:ss}"

You can compare various versions of browsers and which of the HTML5 field types they support using this site.

Due to the current limited support, I recommend you consider using a jquery datetimepicker plugin rather than browsers HTML implementations.



来源:https://stackoverflow.com/questions/32612116/default-datetime-picker-in-visual-studio-2015-showing-only-date-picker-not-allow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!