@Html.LabelFor(model => model.DtNews) @Html.EditorFor(model => model.DtNews)
and in your view model you can use the [DisplayFormat] attribute
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] public DateTime DtNews { get; set; }
Now you will tell me that this class is generated by the EF framework, to which I would answer you: YOU SHOULD NOT USE YOUR AUTOMATED EF MODELS IN YOUR KINDS . You must define and use viewing models. View models are classes that are specifically tailored to the requirements of your views. For example, in this particular view, you have a requirement to format dates in a certain way: ideal for view models:
public class MyViewModel { [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] public DateTime DtNews { get; set; } }
then your controller action can request your repository and get the domain model (auto-generated EF object) and map it to the view model. He will then pass this view model into the view.
source share