Reputation: 5670
My model property is like this
[DisplayFormat(DataFormatString = "{0:MMM dd, yyyy}"), DisplayName("Procedure Effective From"),
DataType(DataType.Date)]
public DateTime ProcedureEffectiveFrom { get; set; }
And in view
@Html.TextBoxFor(model => model.ProcedureEffectiveFrom, new { @class = "form-control date-picker" })
@Html.ValidationMessageFor(model => model.ProcedureEffectiveFrom)
And I am expecting an output like this
1/27/2015
But the output is
1/27/2015 10:40:45 AM
Can anyone point out what I am doing wrong?
Upvotes: 0
Views: 347
Reputation:
Use it
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MMM dd, yyyy}"),DisplayName("Procedure Effective From")]
public DateTime ProcedureEffectiveFrom { get; set; }
in view use like
@Html.EditorFor(model => model.ProcedureEffectiveFrom, new { @class = "form-control date-picker" })
@Html.ValidationMessageFor(model => model.ProcedureEffectiveFrom)
and for displaying the date time use
@Html.DisplayFor(model => model.ProcedureEffectiveFrom)
Upvotes: 1
Reputation: 2623
In view You may try this.
@Html.TextBoxFor(m => m.ProcedureEffectiveFrom, new { htmlAttributes = new { @Value = m.ProcedureEffectiveFrom.ToString("MM/dd/yyyy"), @class = "form-control" } })
Upvotes: 1
Reputation:
Both [DisplayFormat]
and [DataType]
are only respected when using @Html.DisplayFor()
and @Html.EditorFor()
. You need to set the format using the overload that accepts a format string.
@Html.TextBoxFor(m => m.ProcedureEffectiveFrom, "{0:MM dd, yyyy}" new { @class = "form-control date-picker" })
Upvotes: 1