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?