I have a datetime picker in my view:
@Html.EditorFor(m => m.EventDate, new
{htmlAttributes = new {
@Value = DateTime.Today.ToShortDateString(),
@class = "form-control datepicker" }, })
I’ve dropped the css
files in the directory Content
in my project:
It is Twitter Bootstrap
v3.0.1
And my scripts in the directory Scripts
:
Here’s my _Layout.cshtml
<!DOCTYPE html>
<html lang="en">
<head>
@RenderSection("meta", required: false)
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ... -->
<link href="~/Content/themes/flatly/bootstrap.css" rel="stylesheet">
<link href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css"
rel="stylesheet">
<!-- ... -->
@Scripts.Render("~/js")
@RenderSection("scripts", required: false)
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
</body>
</html>
Here’s my Model
public class MyModel
{
[DataType(DataType.Date)]
public DateTime EventDate
{
get;
set;
}
When I execute the web site, the rendering is different on IE
or Chrome
On chrome
On IE
On FireFox
How to I manage the datetime picker on IE
and FireFox
?
2
Answers
@Stephen Muecke pointed the reason of the problem:
The solution I've emplemented to use HTML5 datepicker when exists or use the JQuery datepicker when it doesn't is this one:
In
_Layout.cshtml
I checked this line existed andmodernizr
was installed in my solution (which should be the case by default)In the view with the datepicker, in the
@section Scripts
I addedThe issue as nothing to do with bootstrap. Your rendering the browsers HTML5 datepicker which required the format to be
yyyy-MM-dd
(ISO format)Include the
[DisplayFormat]
attribute on your propertyand in the view, use
Note you should never override the
value
attribute when using a html helper (consider what happens if a user enters a date which is not todays date and submits, but because of aModelState
error you need to return the view for correction – your code would reset the users selection back to today’s date)