简体   繁体   English

如何使用IEnumerable空检查@ Html.DisplayNameFor-C#和ASP.NET MVC

[英]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.Ordermodel.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> . 您的@modelIEnumerable<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.

相关问题 如何使用@HTML.DisplayNameFor 在asp.net mvc 中格式化日期时间变量 - how to format a date time variable in asp.net mvc using @HTML.DisplayNameFor HTML.DisplayNameFor不显示数据(.net MVC) - Html.DisplayNameFor not show data (.net mvc) 将Html.DisplayNameFor与IEnumerable一起使用<IGrouping> - Use Html.DisplayNameFor with IEnumerable<IGrouping> Html.DisplayNameFor List vs IEnumerable in Razor - Html.DisplayNameFor List vs IEnumerable in Razor @model是IEnumerable时,如何在Html.DisplayNameFor(model =&gt; model.ID)中进行建模 <Student> ? - How can model in Html.DisplayNameFor(model=>model.ID) become Student when @model is IEnumerable<Student>? 具有复合ViewModel的MVC和实体框架Html.DisplayNameFor - MVC and Entity Framework Html.DisplayNameFor with Composite ViewModel @ Html.DisplayNameFor()中的Lambda表达式 - Lambda expression in @Html.DisplayNameFor() 如何更改@ Html.DisplayNameFor(model =&gt; model.name)的显示 - How to change the display of @Html.DisplayNameFor(model => model.name) 如何在列表的基类属性的属性上@ Html.DisplayNameFor()? - How to @Html.DisplayNameFor() on a property of a list’s base class property? ASP.NET MVC。 如何使用DisplayNameFor来创建表标题和正文? - ASP.NET MVC. How use DisplayNameFor in order to create a table heading and body?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM