5

I've a project in MVC 3. It works perfectly and each time I build it, it succeeds. However, Visual Studio insist in showing compile errors in views in each razor syntax. For example:

The name 'model' does not exist in the current context D:\ ... Detail.cshtml

That line is simple:

@model Namespace.DetailViewModel

I've already tried:

  • Cleaning solution
  • Cleaning solution and deleting bin and obj folders.
  • Closing VS and reopening it (even with a reboot in the middle).

Note: I'm using Visual Studio 2012.

2
  • Have you tried deleting the User Profile file? Commented Jan 16, 2013 at 14:03
  • No, I didn't think in that, but I finally found it. Commented Jan 16, 2013 at 14:08

2 Answers 2

3

I've solved it!

The project was created when the computer didn't have MVC 4 installed yet. After installing it, for some reason, it kept working (despite this).

So what I did was make the changes that should be made in MVC 3 project after installing MVC 4: see here

After that, it compiled and the errors disappeared, however there was an error with the versions of some libraries (System.Web.WebPages, System.Web.WebPages.Razor, and some others). That turned out to be because they had Copy Local set to True. After changing that everything started working great.

Sign up to request clarification or add additional context in comments.

Comments

0

Open your web.config file located inside of Views folder and add this section if it's missing:

<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" />
    </namespaces>
  </pages>
</system.web.webPages.razor>

2 Comments

How does this fixes the problem?
It includes references used by @model in Views. If you start with a new empty ASP.NET MVC project, Visual Studio doesn't automatically include web.config file in Views folder. I thought that it might be similar problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.