【发布时间】:2019-10-18 09:07:55
【问题描述】:
我不确定我是否搜索错误,但我似乎无法确定如何null 检查@Html.DisplayNameFor。
我有一个IEnumerable<model>,其中有一个model.Numbers model.Order 和model.Time:
@Html.DisplayNameFor(Model => Model.Numbers)
等等
我试过这样做,但 VS 会抛出错误:
@Html.DisplayNameFor(Model => Model?.Numbers)
但是当我将鼠标悬停在 VS 中的红色波浪上时,我收到了这条消息:
表达式树 lambda 可能不包含空传播运算符
我也尝试过附加 where() 和 @Html.DisplayNameFor(Model => Model.Numbers.Any()) 但这些都不起作用。
我的代码:
@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>
型号:
public class NumbersModel
{
public int ID { get; set; }
public string Numbers { get; set; }
public string Order { get; set; }
public string Time { get; set; }
}
【问题讨论】:
-
你遇到了什么错误?
-
An expression tree lambda may not contain a null propagating operator.是当我用?将鼠标悬停在VS 中的红色波浪线上时得到的 -
你可能想阅读这个question接受的答案
-
如果你的
@model是IEnumerable<model>,那么你肯定不能打电话给.Numbers等等。您可能在页面上有一个循环枚举Model,它是您传递给 lambda 的循环变量。 -
我在调用
DisplayNameFor3 次后有一个循环,它遍历模型并通过DisplayFor呈现数据
标签: c# asp.net-mvc