Assembly Not Referenced compilation error in foreach loop in Razor view
Asked Answered
A

3

15

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;
        }
Ambriz answered 11/4, 2015 at 7:19 Comment(3)
possible duplicate of The type is defined in an assembly that is not referenced, how to find the cause?Austronesian
@Bjørn-RogerKringsjå I've added an edit explaining, but I think the challenge here compared to the many other "Not Referenced" SE questions is that they have been primarily dealing with either a) ReSharper "cannot resolve symbol" errors (not using R#), or b) user libraries, as opposed to built-in types list List<T>Ambriz
Did you try to add @using System.Collections.Generic?Austronesian
A
43

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.

Ambriz answered 11/4, 2015 at 7:19 Comment(3)
Work for me. Thx !Collbaith
Notice that you need to surround the <assemblies> annotation with <compilation>. Like this: <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </assemblies> </compilation>`Corneliuscornell
<add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> in my caseCameroun
I
2

I had the same issue recently which is best described here: https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0012

The problem was comming from two different references in PartialView.cshtml and MainView.cshtml, each referring to two different classes in Razor pages; Intersection was a foreach loop in the both views.

Solution was to add one more line:

<add assembly="NameOfTheProject.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

to the already existing assembly info in Web.config under Views folder.

Inceptive answered 9/2, 2021 at 19:15 Comment(0)
S
0

This is because of how references are added in the Razor Engine. This issue is reported https://github.com/Antaris/RazorEngine/issues/415.

Shortcake answered 18/12, 2018 at 3:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.