在的Razor视图foreach循环大会未引用编译错误 [英] Assembly Not Referenced compilation error in foreach loop in Razor view

查看:218
本文介绍了在的Razor视图foreach循环大会未引用编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我已经检查并尝试了很多其他的大会未引用的SE发现的问题,但我还没有发现有很多处理什么应该是一个内置组件( System.Collections中.Generic.List< T> )。这使得难以手动添加或删除的基准等。

我试图建立从API响应PartialView。我已确认的反应是正确的,良好的,我的对象是正确建立,但是当我产生局部视图,代替显示编译错误。

 编译器错误信息:CS0012:类型'System.Collections.Generic.List`1< T0>'在未引用的组件被限定。您必须添加到程序集的引用System.Collections中,版本= 4.0.0.0,文化=中性公钥= b03f5f7f11d50a3a。

下面是的Razor视图:

  @using OpsComponent
@model OpsComponent.ComponentData< D​​IV CLASS =行>
    < D​​IV CLASS =COL-MD-6>
        < UL类=列表组>
            @foreach(在Model.Metrics数据度量)
            {
                <李班=列表组项>
                    <跨度类=徽章> @ metric.Value< / SPAN>
                    @ metric.Key< BR />
                < /李>
            }        < / UL>
    < / DIV>
< / DIV>

这是数据类的定义:

 公共类数据
    {
        公共字符串键{获得;组; }
        公共字符串值{获得;组; }
        公共字符串源{搞定;组; }
        公共状态状态{搞定;组; }    }

如果状态是一个枚举。我在调试该Model对象是正确的,它传递给PartialView之前就形成遏制,但不是一个正确的布局,我得到的服务器错误的屏幕和一个500响应。

在该行 @foreach(在Model.Metrics数据度量)

行动code的完整性:

 公众的ActionResult ComponentDetail(字符串ID)
        {
            VAR数据= Client.GetComponentData(id.De codeBase64ToString());
            变种partialViewResult = PartialView(_ ComponentDetail数据);
            返回partialViewResult;
        }


解决方案

我已经想通了,这是过分地简单。我仍然不知道为什么,这是必要的,但增加了新的组装标记的web.config 似乎有解决了这个问题。我加了标签是在<编译> 标记,如下:

 <&集会GT;
    <添加组件=System.Collections中,版本= 4.0.0.0,文化=中性公钥= b03f5f7f11d50a3a/>
< /组件>

很简单,但解决了错误,并认为现在呈现正确。

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.

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.

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.

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

Action code for completeness:

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

解决方案

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:

<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.

这篇关于在的Razor视图foreach循环大会未引用编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆