3

I recently did a NuGet package-update on two of my projects. So I am using Microsoft.AspNet.Mvc version 5.2.0. One project is working great, the other has something out of whack because I cannot now create strongly typed razor views. Here is a snip from one of my views:

@ModelType   MyServiceLibrary.EmailTemplateModelObject
Dear @Model.FirtName,<br><br>

I get the following error:

'ModelType' is not declared. It may be inaccessible due to its protection level.

Pretty much exactly what what is referenced in this question: asp.net mvc 3 'ModelType' is not declared

I have attempted to implement the recommended fix but no luck. I am using a vb.net environment. Here is what i have in my ~/Views/Web.config:

 <configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
  </configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.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.Optimization"/>
    <add namespace="System.Web.Routing" />
  </namespaces>
</pages>
  </system.web.webPages.razor>

Can anybody see what i'm doing wrong here?

Community
  • 1
  • 1
brando
  • 8,215
  • 8
  • 40
  • 59
  • 1
    It should be `@model MyServiceLibrary.EmailTemplateModelObject` –  Jul 24 '14 at 04:11
  • 3
    I think that is in c# syntax, not vb.net – brando Jul 24 '14 at 04:12
  • 1
    Is that `Web.config` true of all view folders (and any areas you are using)? If you have more Web.configs, check them all to make sure they are similar. – Rowan Freeman Jul 24 '14 at 04:46
  • U are onto something Rowan. The web.config listed above was only in the 'views' directory. I have updated the main web.config with these settings...and now...strange behavior. ModelType is now recognized, but @Model is not. Am getting over 100 'Model is not recognized' compile errors... – brando Jul 24 '14 at 05:00
  • I would leave the main Web.config alone, but check any other Web.config files. – Rowan Freeman Jul 24 '14 at 05:05
  • Among other compile errors, i am now getting: Type 'Nop.Web.Framework.ViewEngines.Razor.WebViewPage' is not defined – brando Jul 24 '14 at 05:18

5 Answers5

4

I had a very similar problem when creating a new MVC 5 project although it was the 'Layout' that was not declared.

I opened the TOOLS > NuGet Package Manager > Manage NuGet Packages for Solutiom item, selected 'Installed packages' and clicked 'Updaet All' which fixed my problems by updating all of the various DLLs and web.config files to the latest version.

Carl Onager
  • 4,112
  • 2
  • 38
  • 66
3

We encountered the same issue under VS 2012. We resolved it by:

NB: Before resetting, I exported the settings; after the reset, I reimported all the settings except for the Web Projects category, and everything still works.

Community
  • 1
  • 1
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
2

In my case, I was adding MVC5 to a project using "Manage Nuget Packages..." that only had WebApi when created. Many other pieces of MVC were missing (RouteConfig.vb, reference to System.Web.Routing, Web.config inside Views folder, ...), but the key to get rid of this issue was ensuring the ~/web.config (the web.config in the root of the site) had an appSettings node that included:

<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
</appSettings>

Once they were added, I restarted VS 2013 and everything worked fine.

Mike Schall
  • 5,829
  • 4
  • 41
  • 47
1

I had the same issue with VS 2013. I closed Visual Studio and re-opened it, problem gone.

Chalky
  • 1,624
  • 18
  • 20
0

Well, I was able to narrow the problem to the subject of "intellisense not working for MVC5" and found a very simple fix that solved my problems:

Step 1: Update webpages:Version

Change

<add key="webpages:Version" value="2.0.0.0" />

to

<add key="webpages:Version" value="3.0.0.0" />

Step 2: Resetting my settings.

Tools > Import and Export Settings... > Reset all settings

Step 3: closed all files, restarted Visual Studio and humpty dumpty was back together again!

Source: Visual Studio 2013 IntelliSense stops working for ASP.NET MVC5 Controllers

Community
  • 1
  • 1
brando
  • 8,215
  • 8
  • 40
  • 59