Assembly Not Referenced compilation error in foreach loop in Razor view

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.


I had the same issue recently which is best described here: https://docs.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.