I'm not sure if I'm searching this incorrectly, but I can't seem to determine as to how to null
check @Html.DisplayNameFor
.
I have an IEnumerable<model>
which has a model.Numbers
model.Order
and model.Time
:
@Html.DisplayNameFor(Model => Model.Numbers)
etc.
I tried doing a this but VS throws an error on:
@Html.DisplayNameFor(Model => Model?.Numbers)
But I get this message when I hover over the red squiggly in VS:
An expression tree lambda may not contain a null propagating operator
I've also tried appending where()
and @Html.DisplayNameFor(Model => Model.Numbers.Any())
but these do not work.
My code:
@model IEnumerable<BusinessLayer.NumbersModel>
...
<table class="table table-sm">
<thead class="thead-dark">
<tr>
<th>@Html.DisplayNameFor(Model => Model.Numbers)</th>
<th>@Html.DisplayNameFor(Model => Model.Order)</th>
<th>@Html.DisplayNameFor(Model => Model.Time)</th>
</tr>
</thead>
@foreach (var item in Model)
{
if (item.Numbers != null && item.Order != null && item.Time != null)
{
<tr>
<td>@Html.DisplayFor(m => item.Numbers)</td>
<td>@Html.DisplayFor(m => item.Order)</td>
<td>@Html.DisplayFor(m => item.Time)</td>
<td><i class="m-icon--edit"></i> @Html.ActionLink("Edit", "Edit", new { id = item.ID }, new { @class = "m-numbers__link" })</td>
<td>
@using (Html.BeginForm("Delete", "Numbers", new { id = item.ID }))
{
<i class="m-icon--delete"></i> <input type="submit" value="Bin" onclick="return confirm('You are about to delete this record');" />
}
</td>
</tr>
}
}
</table>
Model:
public class NumbersModel
{
public int ID { get; set; }
public string Numbers { get; set; }
public string Order { get; set; }
public string Time { get; set; }
}