Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC C# Chrome not showing date in edit mode

I am using Google Chrome V28 as my browser - and have a problem with the DataAnnotations on my model, which force Chrome to use it's own inbuild calendar when rendering a datatime type in my view.

My model is:

public class ObScore
{
    public int ObScoreId { get; set; }

    ...
    ...
    [DisplayFormat(DataFormatString = "{0:dd MMMM yyyy}", ApplyFormatInEditMode = true)]
    [Display(Name = "Date")]
    [DataType(DataType.Date)]
    public DateTime Date { get; set; }
    ...
    ...
}

There is definitely data in the model: Model Picture

...but when displaying in Edit mode, Chrome shows: Chrome View

Is this a known bug in Chrome, and is there anything I can do in my code, to force the date into the form?

Thanks, Mark

like image 413
Mark Avatar asked Aug 16 '13 13:08

Mark


People also ask

What is ASP.NET MVC in C#?

Model View Controller (MVC) MVC is a design pattern used to decouple user-interface (view), data (model), and application logic (controller). This pattern helps to achieve separation of concerns.

Is C# and ASP.NET MVC are same?

They are the same thing. C# is the language you have used to do your development, but ASP.NET MVC is the framework you used to do it.

Is ASP.NET MVC still used?

ASP.NET MVC is no longer in active development.

What is .NET ASP.NET and C#?

ASP.NET is a web application framework developed and marketed by Microsoft to allow programmers to build dynamic web sites. It allows you to use a full featured programming language such as C# or VB.NET to build web applications easily.


1 Answers

Sorry to resurrect a 6 month old question, but I was having the same problem and have an answer that I feel is exactly what the OP was looking for.

While answer from mikhairu does work, it requires changing the type to text. The screenshots by the OP show Chrome's inbuilt datepicker UI, which requires the input to be type=date. Chrome requires the value of an input type=date to be in the format of YYYY-MM-DD, presumably to avoid any culture-specific ambiguity.

Therefore, in your model, add this data annotation to your date properties:

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
like image 140
JiffyLueb Avatar answered Oct 18 '22 04:10

JiffyLueb