0

In my view in asp.net core, I am trying to display my values in a @foreach. Here it is

@foreach (var item in Model)
{
    <tr>
        <td>@item.FirstName</td>
        <td>@item.LastName</td>
        <td>@item.Birthdate.Value.ToString("d")</td>
        <td>@item.Startdate.Value.ToString("d")</td>
    </tr>
}

This throws a database error when there is a null value in Startdate.

If I change @item.Startdate.Value.ToString("d") to

@item.Startdate, I do not get the error anymore. All of my properties are nullable.

My confusion is because I thought that if I use .Value.ToString("whatever") this would ensure there is a value, if so then it would ToString() it, if there is no value then it would leave it null and nothing would happen.

My question is how I can ToString a value in my view if its not null, otherwise ignore?

  • Does this answer your question? [Why does .ToString() on a null string cause a null error, when .ToString() works fine on a nullable int with null value?](https://stackoverflow.com/questions/11446838/why-does-tostring-on-a-null-string-cause-a-null-error-when-tostring-works) – madreflection Jul 28 '20 at 19:50

2 Answers2

2

Try the below code. You can follow the same for Birthdate as well.

@foreach (var item in Model)
{
    var sDate = item.Startdate.HasValue ? item.Startdate.Value.ToString("d") : "";
    <tr>
        <td>@item.FirstName</td>
        <td>@item.LastName</td>
        <td>@item.Birthdate.Value.ToString("d")</td>
        <td>@sDate </td>
    </tr>
}
Sowmyadhar Gourishetty
  • 1,843
  • 1
  • 8
  • 15
2

If your Nullable has no value (HasValue is false) it is an invalid operation to request its Value, and if your code uses your nullable's .Value directly without checking it first you can probably skip using nullables! :)

The idea behind Nullable is to help deal with a situation of having something that cannot be null, and then needing a way to make it nullable because it's "unavoidable" but you then need to check, for every situation you use it, whether it has a value or not and act appropriately

If you want to have a nullable that might or might not have a value, and you want to blind call something on it and know you have a value, then you need something like:

item.Startdate.GetValueOrDefault(DateTime.MinValue).ToString("d")

You have to make a decision as to what value to use if there is no current value. You can shortcut things by using the null propagator against the nullable:

item.Startdate?.ToString("d")

If the nullable has no value this whole thing evaluates to null. If it has a value, it evaluates to a DateTime (or whatever type is in the nullable) that is then ToString'd. You can only really use this in contexts that will tolerate null, such as setting a textbox's text

As it evaluates to null it can be null coalesce'd too:

item.Startdate?.ToString("d") ?? DateTime.MinValue.ToString("d")

The shorter variation of this being to coalesce the nullable datetime with an actual datetime, then tostring the result:

(item.Startdate ?? DateTime.MinValue).ToString("d")

This last form works like the GetValueOfDefault variation earlier, it's just the ?? wasn't a thing until recently; GVOD() is much older

Caius Jard
  • 72,509
  • 5
  • 49
  • 80