【问题标题】:MVC 5 ASP.NET IEnumerable<Item> does not contain a definition for "Inspection" and no extension method "inspection" accepting a first argument of typeMVC 5 ASP.NET IEnumerable<Item> 不包含“检查”的定义,并且没有扩展方法“检查”接受类型的第一个参数
【发布时间】:2017-04-17 04:18:53
【问题描述】:

我正在尝试在我的项目索引视图中显示我的检查模型中的某些数据。他们目前有 M:M 关系。

这是错误

错误 CS1061 'IEnumerable&lt;Item&gt;' 不包含 'Inspection' 的定义,并且没有扩展方法 'Inspection' 接受类型为 'IEnumerable&lt;Item&gt;' 的第一个参数(您是否缺少使用指令还是程序集引用?)

有什么建议吗?谢谢

这是项目索引

@model IEnumerable<NCSafety.Models.Item>

<table class="table">
    <tr>
        <th>
            Hazard Picture
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Hazard.hazName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Inspection.ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.itemCorrActionDue)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.itemCorrActionCompleted)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.itemComment)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @{
                    if (item.imageContent != null && 
                     item.imageMimeType.Contains("image"))
                    {
                        string imageBase64 = 
                        Convert.ToBase64String(item.imageContent);
                        string imageSrc = string.Format("data:" + 
                        item.imageMimeType + ";base64,{0}", imageBase64);
                        <img src="@imageSrc" style="max-height:100px; max-
                        width:120px" class="img-responsive img-rounded" />
                    }
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Hazard.hazName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Inspection.ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.itemCorrActionDue)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.itemCorrActionCompleted)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.itemComment)
            </td>
            <td>
                <a href="/Items/Edit/@item.ID"><div class="glyphicon 
                 glyphicon-pencil" style="margin-right:30px"></div></a>
                <a href="/Items/Details/@item.ID"><div class="glyphicon 
                 glyphicon-eye-open" style="margin-right:30px"></div></a>
                <a href="/Items/Delete/@item.ID"><div class="glyphicon 
                glyphicon-remove"></div></a>
            </td>
        </tr>
                    }

</table>
<table>
    @foreach (var item in Model.Inspection)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SchoolID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.inspDate)
        </td>
    </tr>
    }
</table>
                    if (Model.Count() == 0)
                    {
                        <h3>No Records Found.</h3>
                        <br />
                        <a href="/Items/Create" class="btn ncBtn">Add Item</a>
                        }
                    }

<a href="@Url.Action("DownloadPdf","Home")"> Download PDF</a>  

【问题讨论】:

  • 为什么不把模型引用改成这样的:@model NCSafety.Models.Item?这样您就可以直接访问每个对象模型。

标签: c# asp.net asp.net-mvc linq


【解决方案1】:

如果这是你的模型:

IEnumerable<NCSafety.Models.Item>

那么这些都不起作用:

@Html.DisplayNameFor(model => model.Hazard.hazName)

因为IEnumerable&lt;T&gt; 没有任何这些属性。也许您打算深入了解 IEnumerable&lt;T&gt;元素 的属性?像这样的:

@Html.DisplayNameFor(model => model.First().Hazard.hazName)

(对您的其他属性重复)

【讨论】:

  • 如果您的视图模型是IEnumerable(T) 类型,那么@Html.DisplayNameFor 使用第一个参数为Expression&lt;Func&lt;T&gt;&gt; 的重载。所以你应该能够将model.Inspection 作为表达式,因为 T 是一个项目。
【解决方案2】:

Inspection 位于模型对象或构成模型的集合对象中。它可能不在两者上。

【讨论】:

  • 这是我在 ITEM 模型中用于链接检查表的内容。那是对的吗?公共虚拟检查检查{得到;放; }
  • @user2197867 我认为您走在正确的道路上。您的模型是 IEnumerable - 这是一个集合。因此,您只能访问您收藏中的项目。例如foreach item in Model。你不能做 Model.Inspection,因为你的模型上不存在 .Inspection。它只存在于模型中的项目对象上。
猜你喜欢
  • 2019-07-29
  • 2018-01-28
  • 2015-11-21
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多