In a view that I have modified a lot, I notice that the date format of the Datepickers is not adapted to what I want.
I do not want hours. After research, I discover that it is advisable to add an attribute to the DateTime properties of the model:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MMM yyyy}")]
I then assume that this attribute addition should be applied to the view. But I do not know how to do it.
Can we reload a view so that it takes into account the changes, or should we delete it and build a new one?
Please allow me to share my test within .Net 8 MVC APP and I think this might be of benefit to show the result. Below is my code, and just like what you can see, I have different date format setting on the model, and I have type="date"
configuration on one of the input.
<div>
<label asp-for="MyDate" class="control-label"></label>
<input asp-for="MyDate" type="date" class="form-control" />
<input asp-for="MyDate2" class="form-control" />
</div>
<div>
@Html.TextBoxFor(m => m.MyDate, new { @type = "date" })
@Html.TextBoxFor(m => m.MyDate2, new { })
</div>
public class MyModel
{
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yyyy}")]
public DateTime MyDate { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MMM yyyy}")]
public DateTime MyDate2 { get; set; }
}
Just like what you can see in the screenshot, [DisplayFormat]
makes the value in DOM has different behavior, but it doesn't affect the appearence in UI, it's type="date"
makes the date input to only display date without time. In the meantime, the [DisplayFormat] attribution requires an application restart to take effect.