Razor Views not seeing System.Web.Mvc.HtmlHelper

Viewed 104204

I am in the process of upgrading to MVC4. I have followed the instructions at http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806 but in my Razor views and layouts I have errors like

  • 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'BeginRouteForm' and no extension method 'BeginRouteForm' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

I also have errors like:

  • "The name 'Viewbag' does not exist in the current context

When I hover over @Html I can see it is of type System.Web.WebPages.Html.HtmlHelper not System.Web.Mvc.HtmlHelper

Not really sure what other information is pertinent but this is where I am:

  • I haven't updated class librarys etc.. in the sln to .net 4.5 yet.
  • The project was created in VS2010, but I am doing the migration in VS2012
  • The project opens and runs OK with VS2012, .Net 4.0, MVC 3

Any pointers appreciated.

Edit: All references and files in web.config upgraded to:

  • System.Web.Mvc, Version=4.0.0.0
  • System.Web.WebPages, Version=2.0.0.0
  • System.Web.Helpers, Version=2.0.0.0
  • System.Web.WebPages.Razor, Version=2.0.0.0

Edit(2): In my /views/web.config (or /views/shared/web.config if I try @Paul 's solution below) in the element, I have my own base type which inherits from System.Web.Mvc.WebViewPage , the references in the containing library have been updated to MVC4 and go to definition takes me to the MVC4 dll.

Has anything changed in this area on the new release? I couldn't see anything pertinent in the release notes.

22 Answers

Recently got the same problem and this is how i fixed it: On Visual Studio with your project in question open, Goto:

  1. Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution.
  2. On the open window, select Updates. then Click Update All.

It will load whats missing in your project and all should be back on track.

Right, I've been trying to fix this issue for sometime. I've used all the solutions in the various Stack Overflow topics regarding this and none seemed to be working.

I have just fixed the issue this morning. After you gave fixed the web.config for both the project and the views, making sure all the .dll versions are matching with what you have in the references folder. You will need to unload the project, edit the .csproj, and then update all the .dll versions in that file.

System.Web.Helpers
System.Web.Mvc
System.Web.WebPages

Hope this helps, as I have finally fixed this issue! No more red squiggly lines.

This has also fixed the context menu issue I was having where, I wasn't getting the option to add a controller, view etc.

Related