Rory McCrossan
Rory McCrossan

Reputation: 337580

The type or namespace name 'X' does not exist in the namespace 'Y' - in VS generated code

This is the weirdest error I have ever encountered.

This MVC web project was working fine until today. It hasn't been worked on by anyone in a couple of weeks.

Even though NOTHING has changed, simply running it now results in:

The type or namespace name 'ColourboxViewModel' does not exist in the namespace 'CMSModels.ViewModels'

(x5)

The type or namespace name 'Colourbox' does not exist in the namespace 'CMSModels' (are you missing an assembly reference?)

(x1)

There is a reference to CMSModels in the project. Removing this reference gives me about 200 extra errors for each other class in the namespace, so I know it is working, but not for these two errors.

This is the line of generated code which the error points to:

public class views_colorbox_index_aspx : System.Web.Mvc.ViewPage<IEnumerable<CMSModels.Colourbox>>, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
...
public class views_colorbox_content_ascx : System.Web.Mvc.ViewUserControl<CMSModels.ViewModels.ColourboxViewModel> {

Intellisense is picking up the location of both CMSModels.ColourBox and CMSModels.ViewModels.ColourboxViewModel.

I am completely stumped. Any help greatly appreciated as I am completely at a loss.


OK I have found the issue - but not the solution.

The class is defined as

public class ColourBoxViewModel

yet the generated code is looking for a class of 'ColourboxViewModel'. Note the lowercase B. Where does the auto-gen code get the names of classes from? Search in project reveals no mentions of it with a lowercase b.

The issue is the same for the ColourBox class.


Problem solved - and @František Žiačik you were correct. There was a folder which had been excluded from the project (which is why searching in entire project and within the views in visual studio found nothing) which contained the incorrectly typed inherit statement. The excluded folder was called ColorBox when originally created by outsourcers. This had been copied and renamed as ColourBox by another dev on the team who decided to, and I quote, 'spell everything correctly'. Said dev is now only allowed to code using a Speak n' Spell.

Upvotes: 6

Views: 10682

Answers (2)

JerryA
JerryA

Reputation: 163

I had this error yesterday, and I found these answers, but in my case they didn't help. Instead it turned out that in my client in project properties the Target framework was set to .NET Framework 4 Client Profile. Solution was to change this to full .NET 4. Hope this can help someone, because the error in VS is not very helpfull.

Upvotes: 5

František Žiačik
František Žiačik

Reputation: 7614

So here is my guess.

In your content.ascx view (or whatever it's named) you have declared:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CMSModels.ViewModels.ColourboxViewModel>" %>

instead of

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CMSModels.ViewModels.ColourBoxViewModel>" %>

Upvotes: 5

Related Questions