I'm attempting to create a strongly-typed view based on a class from another assembly. For whatever reason though, my Razor view doesn't seem to have any visibility of other assemblies referenced on my project. e.g.
@model MyClasses.MyModel
results in the error in Visual Studio 2010, "The type or namespace name MyClasses
could not be found (are you missing a using directive or an assembly reference?)."
The same class referenced in the standard view engine works fine. I have the same trouble trying to reference the class in the body of my view.
Am I missing something about Razor or do I need to reference the assembly some other way?
well, for me it was different. I was missing assembly of my console application project with MVC project. So, adding reference was not enough.
well this might help someone else. go to root web.config file
system.web
->compilation
-> add your project reference like this.<assemblies> <add assembly="Your.Namespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> </assemblies>
For me I was referencing a project that was a console application. It was set to build as an exe (console application) instead of class library (DLL). When I changed this I was able to see the models from that separate project no problem.
I also had the same issue, but the problem was with the Target framework of the assembly.
The referenced assembly was in .NET Framework 4.6 where the project has set to .NET framework 4.5.
Hope this will help to someone who messed up with frameworks.
I was getting the same error while trying to use Smo objects in a Razor view. Apparently this is because Razor can't find the DLLs referenced in the project. I resolved this by setting "Copy Local" to true for all Smo dlls, however there might be a better solution (see Czechdude's link above) @using and web.config edits are useless because they are only needed if you wish to omit the namespace part from the type names (for instance Server instead of Microsoft.SqlServer.Management.Smo.Server)
None of the above worked for me either;
But I finally found something that worked for me:
It was because I had my Build Output going to bin\Debug\ for Debug configuration and bin\Release\ for Release configuations. As soon as I changed the Build Configuation to "bin\" for All configuations (as per image below) then everything started working as it should!!!
I have no idea why separating out your builds into Release and Debug folders should cause Razor syntax to break, but it seems to be because something couldn't find the assemblies. For me the projects that were having the razor syntax issues are actually my 'razor library' projects. They are set as Application Projects however I use them as class libraries with RazorGenerator to compile my views. When I actually tried to run one of these projects directly it caused the following Configuration error:
This lead me to trying to change the Build Output, as I notice that for all web projects the Build Output seems to always be directly to the bin folder, unlike the default for class libraries, which have both release and debug folders.
You seem to be looking for this answer: https://stackoverflow.com/a/4136773/176877
That is, open the inner Views\Web.Config (NOT the root one), and add the namespace under the Pages tag:
Save that, then close and reopen the Razor file.
If you're using Areas you'll need to do this for each Web.Config in each Area.
Visual Studio has gotten buggier over the years so it may require closing the Razor file running a Debug build then reopening the Razor file, or may in the worst require restarting Visual Studio. But ultimately it will present the Razor file to you as though everything in that namespaces list was in @using statements at the top of all your Views.