In my view I have a date field (which used for Jquery Date Picker),
@if (Model != null)
{
if (Model.SubmitStartDate == DateTime.MinValue)
{
Model.SubmitStartDate = null;
}
@Html.TextBoxFor(m => m.SubmitStartDate , new { @Id = "SubmitStartDate", @readonly = "readonly", @Value = @Model.SubmitStartDate})
}
In my Model I have :
[Required(ErrorMessage = "Please select a start date")]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime? SubmitStartDate { get; set; }
However in UI I get 10/07/2013 00:00:00 on load , How can I remove the time part from this. Also I require date in the format "dd-mm-yy". Is it possible to achieve?
UPDATE
I edited my code as per the answers, and I got it worked as per requirement, however I got a problem.
When I use @Html.EditorFor()
my readonly
property will not work any more. I tried with DisplayFor
too, however it doesn't make sense as I was using it for a J-query Date Picker. Is there any way I can make that field to read only?
updated Code:
@if (Model != null)
{
if (Model.SubmitStartDate == DateTime.MinValue)
{
Model.SubmitStartDate = null;
}
@Html.EditorFor(m => m.SubmitStartDate , new { type = "date", @Id = "SubmitStartDate", @readonly = "readonly", @Value = @Model.SubmitStartDate})
}
You are using a @Html.TextBoxFor()
helper which will ignore your DisplayFormat
attribute. Change it to a @Html.EditorFor()
and your DisplayFormat will be applied.
And to get your format:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yy}")]
public DateTime? SubmitStartDate { get; set; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With