Reputation: 750
Perhaps this is an Intellisense issue, as it only occurs in .cshtml
files and the code compiles/executes normally. I'm not sure.
I recently migrated from VS2010 to VS2017 and I'm trying to work with an MVC3 project. Everything runs fine, but I'm getting multiple Intellisense errors related to the MVC syntax. For example in Views > Shared > _Layout.cshtml:
<title>@ViewData["Title"]</title>
Gives the error:
The name 'ViewData' does not exist in the current context
This is set by Views > Home > Index.cshtml:
@{
ViewData.Title = "Dashboard"; // intellisense error also shows here
}
This similarly occurs when trying to use @Html.Partial
:
'HtmlHelper' does not contain a definition for 'Partial' and no extension method 'Partial' accepting a first argument of type 'HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
I'm not sure if there's some sort of issue with my dll references or web config. I've verified the following dll references are present:
System.Web.Mvc - Version 3.0.0.0
System.Web.Razor - Version 2.0.0.0
System.Web.WebPages - Version 1.0.0.0
System.Web.WebPages.Razor - Version 1.0.0.0
In my Views/web.config file:
<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" />
<add namespace="System.Web.Optimization" />
</namespaces>
</pages>
</system.web.webPages.razor>
And my root web.config file:
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
Upvotes: 2
Views: 3377
Reputation: 189
I recently migrated from VS2017 to VS2019. I was get the following error: "The name 'ViewData' does not exist in the current"
I was re-run the visual studio launcher and click on modify. Then the problem was solved!
I hope this answer helps you.
Upvotes: 0
Reputation: 750
Ended up using a solution posed here (even though it's VS2013):
MS says that for VS2013 "Intellisense for Razor (CSHTML and VBHTML) files is limited to HTML markup."
But if you add these two lines inside each .cshtml the intellisense will work again for MVC3 in VS2013:
@using System.Web.Mvc.Html
@inherits System.Web.Mvc.WebViewPage<dynamic>
Instead of dynamic you can put your Model's type.
Upvotes: 2
Reputation: 77896
Though am not exactly sure why it's behaving so but you should consider using ViewBag
instead which is a dynamic wrapper over ViewData
.
ViewBag.Title = "Dashboard"; // in your action method
In your view code
@ViewBag.Title
Upvotes: 0