Formatting nullable DateTime fields in strong typed View
Asked Answered
C

5

2

I have a Person class with a BornDate property in my model defined as

[DisplayName("Born Date")]
public DateTime? BornDate { get; set; }

I use this field in my view as

<td style="white-space:nowrap">
    <%= Html.LabelFor(model => model.BornDate)%>
    <br />
    <%= Html.TextBoxFor(model => model.BornDate, new { id = "bornDate" })%>
    <%= Html.ValidationMessageFor(model => model.BornDate, "*")%>
</td>

The problem is that when I am editing a Person instance with the BornDate text box is formatted as

dd/MM/yyyy hh.mm.ss

while I would like to format it without the time part ("dd/MM/yyyy"). I am not able to use the toString method with the format string because it is a nullable field.

what can I do?

Cephalalgia answered 10/9, 2010 at 10:55 Comment(2)
Good example also here stackoverflow.com/questions/12633471Entophyte
Good example about same problem can be found here stackoverflow.com/questions/12633471Entophyte
L
11

You should be able to use Value. Just check it isn't null first.

var displayDate = model.BornDate.HasValue ? model.BornDate.Value.ToString("yyyy") : "NoDate";
Leet answered 10/9, 2010 at 11:20 Comment(3)
First of all thanks for your response. Where do I have to put this code? If I put it inside the TextBoxFor function, within the lambda expression, I get an InvalidOperationException "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."Cephalalgia
Hrm... yeah I forgot the fact you are using strongly typed helpers. I think the only way round this is to use the TextBox helper: <%= Html.TextBox("date", Model.BornDate.Value.ToString("yyyy"), new { id = "bornDate" }) %>Leet
if anyone has issue to display date in View check this instead of DisplayForAnnorah
P
8

I know this is very late but I was researching another problem and came across this issue. There is a way to only show the date part which does not require presentation layer formatting.

[DisplayName("Born Date")]
[DataType(DataType.Date)]
public DateTime? BornDate { get; set; }

Using this will ensure that everywhere you bind to this property on your View, only the date will show

Hope this helps somebody

Phoebe answered 31/1, 2011 at 11:29 Comment(2)
Doesn't work in current versions of Razor (4+); can't speak to older ones.Bynum
this doesn't work on all Html helpers, specifically TextBoxForCementite
S
6

To do this in your VIEW, you can use Razor syntax, as well:

 @Html.TextBox("BornDate", item.BornDate.HasValue ? item.BornDate.Value.ToShortDateString():"") 
Symbolics answered 28/10, 2011 at 16:34 Comment(1)
How should I use for DisplayFor?Boy
T
2

You can do this, it will work with Nullable model types:

@model DateTime?
@{
    object txtVal = "";
    if (Model.HasValue) 
    {
        txtVal = Model.Value;
    };
}
@Html.TextBoxFor(x => Model, new {Value = string.Format(ViewData.ModelMetadata.EditFormatString, txtVal))
Tupler answered 6/4, 2011 at 9:4 Comment(0)
E
0

Another way I've tackled this issue is by adding format to the TextBoxFor

@Html.TextBoxFor(model => model.BornDate, "{0:MM/dd/yyyy}", new { id = "bornDate" })
Errant answered 21/6, 2019 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.