Hi Team
I have a model, but to see the view its not showing correct format of date. I need some help to it.
// model
C#
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime AuditDate { get; set; }
// view
@Html.LabelFor(model => model.AuditDate, htmlAttributes: new { @class = "col-md-2 col-form-label text-md-right" })
@Html.TextBoxFor(model => model.AuditDate, "{0:MM/dd/yyyy}", new { @class = "form-control datepicker" })
@Html.ValidationMessageFor(model => model.AuditDate, "", new { @class = "text-danger" })
Prasad RaveendranRank #320 · 7.8K reputationPosted Feb 28, 2024, 6:53 PM EST
Try the below options and see whether this would help
Ensure that your model is valid. If there are any validation errors, it might affect the display format. You can check the model state in your controller action before rendering the view:
if (!ModelState.IsValid) { // Handle validation errors return View(model); }Try adjusting the format directly in the
TextBoxFormethod in your view:@Html.TextBoxFor(model => model.AuditDate, "{0:MM/dd/yyyy}", new { @class = "form-control datepicker" })Instead of
TextBoxFor, you can try usingEditorFor:@Html.EditorFor(model => model.AuditDate, new { htmlAttributes = new { @class = "form-control datepicker" } })Guest UserRank #877 · 2.5K reputationPosted Feb 29, 2024, 1:17 AM EST
Hi Prasad
Thanks so much, it now display, issue was using TextBoxFor instead of EditorFor.