[英]How to null check @Html.DisplayNameFor with IEnumerable - C# & ASP.NET MVC
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
. 我不确定是否搜索错误,但是我似乎无法确定如何对
@Html.DisplayNameFor
进行null
检查。
I have an IEnumerable<model>
which has a model.Numbers
model.Order
and model.Time
: 我有一个
IEnumerable<model>
其具有model.Numbers
model.Order
和model.Time
:
@Html.DisplayNameFor(Model => Model.Numbers)
etc. 等等
I tried doing a this but VS throws an error on: 我尝试这样做,但VS抛出错误:
@Html.DisplayNameFor(Model => Model?.Numbers)
But I get this message when I hover over the red squiggly in VS: 但是当我在VS中徘徊在红色波浪状上方时,我得到了以下消息:
An expression tree lambda may not contain a null propagating operator
表达式树lambda不得包含空传播运算符
I've also tried appending where()
and @Html.DisplayNameFor(Model => Model.Numbers.Any())
but these do not work. 我也尝试附加
where()
和@Html.DisplayNameFor(Model => Model.Numbers.Any())
但是它们不起作用。
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; }
}
Your @model
is an IEnumerable<T>
. 您的
@model
是IEnumerable<T>
。 It does not have .Numbers
or .Order
, the objects it contains do. 它没有
.Numbers
或.Order
,它包含的对象没有。
DisplayNameFor()
only needs to know the type of the expression to get the attribute DisplayAttribute
from. DisplayNameFor()
只需要知道表达式的类型即可从中获取属性DisplayAttribute
。 The expression you pass to it does not have to calculate to a non-null instance of the object, it will not be executed for its result. 您传递给它的表达式不必计算为该对象的非null实例,也不会为其结果执行该表达式。
So 所以
<thead class="thead-dark">
<tr>
<th>@Html.DisplayNameFor(m => m.First().Numbers)</th>
<th>@Html.DisplayNameFor(m => m.First().Order)</th>
<th>@Html.DisplayNameFor(m => m.First().Time)</th>
</tr>
</thead>
This will work even if .Numbers
is null, or .First()
is null, or the entire Model
is null. 即使
.Numbers
为null或.First()
为null或整个Model
为null,这也将起作用。
You generally do not need to handle nulls when passing expression trees to the ASP.NET MVC helpers. 将表达式树传递给ASP.NET MVC帮助器时,通常不需要处理null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.