40

I´m starting to use "MVC 3" but I´m facing some little problems. In my Views, when I code something like this:

@if(Request.IsAuthenticated) {
    <text>Welcome <b>@Context.User.Identity.Name</b>!
    [ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
    @:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
}

The objects like @Request and @Html is indicating an error: The name 'HTML' does not exist in the current context.

The same occurs with @Context, @ViewBag, @Layout, @Url and others.

See:

image

But the code is correctly compiled with no errors. The problem is that I cannot use the Intellisense with theses objects in the Views. Is it normal? (I don´t think so). What could be happening?

I have reinstalled the MVC 3 framework but the same still occurs.

Note: this is a new project from scratch, not a MVC 2 migration. This occurs both with Razor engine and ASPX.

This is the Web.Config in the Views folder:

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.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=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>

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

  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>

    <!--
        Enabling request validation in view pages would cause validation to occur
        after the input has already been processed by the controller. By default
        MVC performs request validation before a controller processes the input.
        To change this behavior apply the ValidateInputAttribute to a
        controller or action.
    -->
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

Thanks!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
outlookrperson
  • 2,761
  • 7
  • 32
  • 49
  • Did you try this solution? http://stackoverflow.com/questions/2326312/name-html-nor-name-model-exist-in-current-context-in-usercontrol-mvc-and-c/2326559#2326559 – Daniel Liuzzi Jul 01 '11 at 13:46
  • 4
    thanks for your answer. I already have seen this post. I think this does not apply to me. I do not have compilation errors and the project is not a previous MVC framework migration. – outlookrperson Jul 01 '11 at 13:55
  • Oh, I see. Sorry about that. Personally I have not experienced this issue, so I can't think of anything else to try other than reinstalling Visual Studio. – Daniel Liuzzi Jul 01 '11 at 14:09
  • I'm experiencing the same issue. Compilation works fine, but this problem takes away Intellisense from Visual Studio. Happening both in 2010 and 2012, and both with MVC3 and MVC4. – Isaac Llopis Dec 17 '12 at 11:04
  • 1
    I'm using Visual Studio 2013 with MVC5, and restarting Visual Studio and then cleaning and building my project solved this problem. Restarting it brought back the Razor intellisense, and cleaning it caused the op's error on all of my Razor calls, until I built both the solution and project. – Ian Campbell Jan 05 '14 at 03:56
  • Same Issue , cleaning and rebuilding worked fine ... – vinayak hegde Dec 02 '15 at 06:18
  • Can't believe this is still a problem with VS 2017 – Mikee Jun 20 '19 at 16:26

20 Answers20

30

You could try:

  1. Close the View with the false errors.

  2. (Steps 2-5 are sometimes optional) Close Visual Studio

  3. Delete the bin and obj folders in all the Projects in the Solution.

  4. Reopen the same Solution

  5. Rebuild the Solution

  6. Open a different View then the one causing the errors

  7. Close that View, hopefully you didn't see any of the similar errors in this View

  8. Reopen the View that gave you problems earlier

Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
  • I was having a similar issue to others here, and oddly enough just opening other views in the project resolved the problem. The procedure was not as elaborate as the one described here. – Mike Strother May 06 '13 at 15:32
  • 5
    It's kind of silly that I actually spent 5 minutes for such issue and the solution is just close the view, open another view, and open the view that caused problem awhile ago, and the errors all gone. I didn't even close visual studio. – Koo SengSeng May 28 '16 at 04:36
  • 8
    Sounds a bit like a VODO ritual, just missing the sacrifice of a black rooster – QT-1 Sep 15 '16 at 15:18
  • This worked for me, I had used resharper to do a refactor and this openned a lot of files. I just closed all files and the errors went away. (VS2015) – robasta Sep 17 '16 at 08:44
  • 5
    Why is this still a bug, 8 years later in VS 2019 (16.6.2)??? At least the solution boils down to simply "have you tried turning off and on again?" – JakeMc Jun 24 '20 at 08:12
  • 1
    Couldn't believe it – coldistric May 20 '21 at 05:23
  • The fact that this is still applicable in 2023 to the latest VS2022 is disgusting! – CrazyPyro Mar 17 '23 at 16:05
8

Clean your solution and Under references. Then set the follow property:

System.Web.MVC file to Copy Local = True.
Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Manu
  • 81
  • 1
  • 1
8

I have solved this issue with the old, good, wise Microsoft default solution: reinstall all the things again.

Uninstall and Reinstall the Visual Studio 2010 and MVC 3 Framework.

outlookrperson
  • 2,761
  • 7
  • 32
  • 49
5

All I had to do was close all views that were open in the editor and rebuild.

steveareeno
  • 1,925
  • 5
  • 39
  • 59
1

What worked for me was closing Visual Studio, deleting the user option files (both solution and project level), then relaunching Visual Studio.

dChimento
  • 2,861
  • 1
  • 17
  • 10
1

I'm using dotnet core sdk 2.2 and Visual Studio Professional 2019.

This was caused by Visual Studio mangling code.

Code was pasted into the razor view. Visual Studio would attempt but then fail to auto-format it. Part of the failure resulted in deleting code.

Pressing ctrl+z reverted the formatting but kept the pasted code. The razor compilation errors were then fixed (The name 'Html' does not exist in the current context)

Disable format on paste (see https://stackoverflow.com/a/28053865/1462295 ) under Tools -> Options -> Text Editor -> HTML -> Advanced

BurnsBA
  • 4,347
  • 27
  • 39
0

For me this just seemed to be the fact that I had compiler warnings. Code would still compile and run ok but it was not until I fixed all the build warnings that my Intellisense starting working.

craigvl
  • 280
  • 2
  • 9
0

I tried to remove the project that is still in trouble from solution and add it back again, after which the problem had been gone.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Pranithan T.
  • 323
  • 1
  • 6
  • 14
0

Set the property of System.Web.MVC,Copy Local = True

Anup Shetty
  • 571
  • 8
  • 10
0

In my case my Packages folder was missing including MVC and Razor so I have updated packages in packages.config, reopened the view and it worked.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
0

All you need to do is Restart Visual Studio and check.

0

You could try some following point:

  • Close the View with the false errors.
  • Close visual studio.
  • open project again.
  • clean your solution
  • Rebuild your solution.

hopefully you will not get any of the similar errors now.

If you still have same problem. than Please check following steps.

  • Make sure you have web.config file inside view folder. Becuase MVC has two web.config. one for solution and other one for project.

Your web.config file will seems like this which is inside views folder.

<?xml version="1.0"?>

<configuration>
  <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.2.7.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" />
        <add namespace="Your Project Name" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

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

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>

  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

It worked for me. and hope for you also.

Love Pandey
  • 330
  • 2
  • 9
0

Close Visual Studio => Delete the obj and bin folders located in your project => Start Visual Studio

hmzn
  • 321
  • 2
  • 6
  • 22
  • After trying everything else, messing with nuget, etc - this answer was what actually worked for me (mvc4 in my case) – imjosh Apr 19 '21 at 22:46
-1
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>

solution for this problem in web.config

-1

I experienced this on ASP.NET MVC 4 as well, after uninstalling EntityFramework from my packages list.

I had to remove this section

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, ....

that's left on the Web.config file... at the least it removed the errors from the errors list

AceMark
  • 701
  • 1
  • 11
  • 21
-1

Apologies for the necro post.

Selecting "Build | Rebuild Solution" corrected this issue for me in Visual Studio 2015. In my case, the warnings occurred after renaming the primary namespace of a project. A rebuild set everything straight.

George N
  • 144
  • 5
-1

I'm using ASP .net core. Solved mine by upgrading Microsoft.AspNetCore.Mvc from 1.1.2 to 1.1.3.

Lawrence
  • 334
  • 2
  • 12
-1

For me, I just restarted my Visual Studios and everything got fixed.

Adeel Asghar
  • 217
  • 2
  • 5
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment](http://stackoverflow.com/help/privileges/comment) on any post. Also check this [what can I do instead](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – thewaywewere Jun 03 '17 at 06:26
  • @thewaywewere Actually, I disagree. His answer was to restart Visual Studio. Sometimes VS does have bugs like this that are solved this way. This is one of those cases, depending on if you say, target a different MVC version, like the OP, since it does cache if you ran it with the previous version, upgraded, and it doesn't take the new settings. – vapcguy Oct 22 '18 at 20:58
-1

Maybe I am a bit late to answer this question but this easy fix helped me:

Right click the file > Exclude from project. Right click the file > Include in project.

Ron Splinter
  • 151
  • 3
  • 14
-2

I had this same issue in MVC 4. None of these solutions worked for me. Instead, in Windows, I went into Control Panel -> Uninstall a program. Select Microsoft ASP.NET MVC 4 in the program list. Click 'Uninstall.' A 'Microsoft ASP.NET MVC 4 Setup' wizard will display. Click 'Repair.'

Jason Rae
  • 2,583
  • 5
  • 25
  • 34