MVC3 Views unable to find added referenced class
Asked Answered
S

4

6

My view is having issues finding a class located in one of my references. This reference is to a dll built outside of the project.

The view always gives error:

The type or namespace name 'Company' could not be found (are you missing a using directive or an assembly reference)

Here is the controller. No issues there.

using Company.Entities;

public ActionResult Find()
{
    Person test = Person.SelectByADDistinguishedName("L*", false);
    return View(test);
}

Here is the View. The error occurs in the @Model line.

@model Company.Entities.Person

@{
    ViewBag.Title = "Bob";
}

<h2>Find</h2>

My Views/Web.config currently looks like this

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="Company.Entities" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

I've checked similar threads like this one but to no avail.

Here is the message on the screen

Line 25:     using System.Web.Mvc.Html;
Line 26:     using System.Web.Routing;
Line 27:     using Company.Entities;
Line 28:     
Line 29:     


Source File: c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET

Files\root\49c9c7db\1f9dd5c8\App_Web_find.cshtml.a8d08dba.xro6gxci.0.cs Line: 27

If I strip out any mention of the assembly (out of web.config - no @using statments). I get the same error message when loaded but against this line

public class _Page_Views_Home_Find_cshtml : System.Web.Mvc.WebViewPage<Company.Entities.Person> {
Skiascope answered 7/3, 2011 at 17:54 Comment(7)
did you try closing your view and reopening it? Or are you getting the yellow screen of death when browsing your view?Brittan
Compilation doesn't error out on syntax errors in views, are you sure the problem is in the view?Brittan
The error is still - CS0246: The type or namespace name 'Dfait' could not be found (are you missing a using directive or an assembly reference?).Skiascope
Added more info to the ticketSkiascope
The compilation problem doesn't exist in the view -- to what section of code does the error refer?Brittan
When the view is open, the line @model Company.Entities.Person (Company is underlined) and the error show in the error list is the one i've mentionned above (the type or namespace name "Company" could not be found..Skiascope
This still happens in vs 2012 + MVC 4. Solved by @Lareau's solution.Burgwell
S
7

After working it with it for awhile, I ended up solving it.

I had to add the assembly under the assemblies section of the main web.config (not the one under the views)

Sample of the web.config.

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="Company, Version=2.0.31.0, Culture=neutral, PublicKeyToken=df9010405db60e6d"/>
  </assemblies>
</compilation>

Thanks for people who gave me suggestions.

Skiascope answered 7/3, 2011 at 19:48 Comment(1)
For some reason, even this doesn't work for me. It's driving me nuts that such a basic thing seems impossible.Parasynapsis
B
4

Be sure to check your views directory for an additional web.config file. I seem to remember running into a similar problem because I hadn't updated the correct web.config. YMMV.

Brigidbrigida answered 7/3, 2011 at 17:58 Comment(1)
Hi Hal, thanks for the quick response, I've added the content of my views/web.config. I've added the namespace to both the views/web.config and /web.config but no luck.Skiascope
P
2

Adding the reference

before where you use your control, in .cshtml file works. Example:

@model IEnumerable<MyApp.Models.MyModel>
@using System.Web.UI.WebControls;

But you can put it in web.config, under namespaces tag. This way it is global to all views. Also, there is no need to specify the version:

<namespaces>
        <add namespace="System.Web.UI.WebControls" />
</namespaces>
Promptbook answered 6/5, 2014 at 14:20 Comment(0)
R
-1

You may not have specified the directory where your model is located as a reference.

Example:

@using YourProjectName.Models

@model YourModelName

Regardant answered 24/6, 2024 at 11:54 Comment(1)
This question has already been answered. The accepted answer is several years old at this point.Snapper

© 2022 - 2025 — McMap. All rights reserved.