在Razor视图中的foreach循环中,程序集没有引用编译错误

时间:2021-03-26 07:09:50

EDIT: I have checked and attempted a lot of the other Assembly Not Referenced issues found on SE, but I haven't found many dealing with what should be a built-in assembly (System.Collections.Generic.List<t>). This makes it difficult to manually add or remove the reference etc.

编辑:我检查并尝试了很多在SE上没有发现的其他程序集,但是我没有发现很多处理应该是内置程序集的程序集(System.Collections.Generic.List )。这使得手动添加或删除引用变得困难。

I am trying to build a PartialView from an API response. I have confirmed the response is correct and well-formed, my objects are being built correctly, but when I generate the Partial View, a Compilation Error is instead shown.

我正在尝试从API响应构建局部视图。我已经确认了响应是正确的和格式良好的,我的对象正在被正确地构建,但是当我生成部分视图时,会显示一个编译错误。

Compiler Error Message: CS0012: The type 'System.Collections.Generic.List`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Here is the Razor view:

这是剃须刀视图:

@using OpsComponent
@model OpsComponent.ComponentData

<div class="row">
    <div class="col-md-6">
        <ul class="list-group">
            @foreach (Data metric in Model.Metrics)
            {
                <li class="list-group-item">
                    <span class="badge">@metric.Value</span>
                    @metric.Key<br/>
                </li>
            }

        </ul>
    </div>
</div>

And here is the Data class definition:

这是数据类的定义:

public class Data
    {
        public string Key { get; set; }
        public string Value { get; set; }
        public string Source { get; set; }
        public Status Status { get; set; }

    }

Where Status is an enum. I have checked in Debugging that the Model object is correct and well-formed before it is passed to the PartialView, but instead of a correct layout, I get the Server Error screen and a 500 response.

其中状态是enum。我在调试中检查了模型对象是正确的,并且在它被传递到PartialView之前是格式良好的,但是我没有正确的布局,而是得到了服务器错误屏幕和500个响应。

at the line @foreach (Data metric in Model.Metrics)

在@foreach行(模型。metrics中的数据度量)

Action code for completeness:

行动代码完整性:

public ActionResult ComponentDetail(string id)
        {
            var data = Client.GetComponentData(id.DecodeBase64ToString());
            var partialViewResult = PartialView("_ComponentDetail", data);
            return partialViewResult;
        }

1 个解决方案

#1


34  

I have figured it out, and it was devilishly simple. I still don't know why this is necessary, but adding a new assembly tag to web.config seems to have resolved this issue. The tag I added was under the <compilation> tag and as follows:

我已经弄明白了,而且非常简单。我仍然不知道为什么需要这样做,但是在web上添加一个新的组装标记。配置似乎已经解决了这个问题。我添加的标签在< compile >标签下,如下:

<assemblies>
    <add assembly="System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>

Simple, but has resolved the error and the view now renders correctly.

很简单,但是已经解决了错误,现在视图呈现正确。

#1


34  

I have figured it out, and it was devilishly simple. I still don't know why this is necessary, but adding a new assembly tag to web.config seems to have resolved this issue. The tag I added was under the <compilation> tag and as follows:

我已经弄明白了,而且非常简单。我仍然不知道为什么需要这样做,但是在web上添加一个新的组装标记。配置似乎已经解决了这个问题。我添加的标签在< compile >标签下,如下:

<assemblies>
    <add assembly="System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>

Simple, but has resolved the error and the view now renders correctly.

很简单,但是已经解决了错误,现在视图呈现正确。