Razor page does not contain definintion of Html helper class - asp.net-mvc

I am currently working on an MVC project. The project is working fine on another colleagues machine but I am having trouble with this project. It constantly gives errors with Html helper classes.
"System.Web.WebPages.HtmlHelper does not contain a definition for 'TextboxFor' does not contain definition and no extension method 'TextBoxFor' accepting argument of type System.Web.WebPages.HtmlHelper could be found."
I have googled this problem but have not found any useful solution.
Here are some steps that I tried to solve this problem.
Clean solution and rebuild it.
removed bin and obj folder and rebuild the solution.
checked references in web.config files (both)
Added refrences in web.config like System.Web.Mvc etc..
Copied packages from other colleagues' machine.
Note: I am using visual studio 2013 with MVC 5

Sounds like you're missing the following in the Web.Config in the views folder:
/Views/Web.Config
<?xml version="1.0"?>
<configuration>
<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"> // <-- this line and contents are important
<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>
Views typically derive from System.Web.Mvc.WebViewPage which is configured in the web.config. Make sure the version above matches the version you need.

Related

VS 2013 MVC 5 No Intellisense for HtmlHelpers in Area Views

Project: VB.NET MVC5 w/ EF6, MvcScaffolding, and BootstrapEditorTemplates
Since this morning I don't get any intellisense for any of the built in MVC helpers, nor my custom ones in any of the Views in an Area.
NOTE: I can still see Html.Encode, and Htm.DropDownList (NOT DropDownListFor<>) etc.
If I build it I recieve errors, though I can still browse the application correctly and see the correct editors and data.
#ModelType Quotes4YouVB.Request
#Html.EditorFor(Function(model) model.Urgency)
#Html.EditorFor(Function(model) model.Message)
I recieve the following errors among other similar ones.
'ModelType' is not declared. It may be inaccessible due to its
protection level. 'ViewBag' is not declared. It may be inaccessible
due to its protection level. 'EditorFor' is not a member of
'System.Web.WebPages.Html.HtmlHelper'. 'ValidationMessageFor' is not a
member of 'System.Web.WebPages.Html.HtmlHelper'.
I've tried all the usual suspects:
Close and Re-open View
Restart VS
Delete /bin and /obj
Tools / Imports and Export Settings / Reset All Settings
Uninstall MvcScaffolding, T4Scaffolding and T4Scaffolding.Core
Re-Installed MVC 5.2 to project through NuGet
NuGet: Update-Package
Any ideas anyone? It seems odd that it's specific to /Areas/, it works completely fine in ~/Views/X/Y.vbhtml
Found the solution! I'm unsure why this specific thing causes this problem, but as with most VS issues I'm not gonna question it too much, just gonna go with it haha.
It relates to the System.Web.WebPages.Razor Host FactoryType section.
My ~/Views/Web.config has the following:
<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>
...
</namespaces>
</pages>
</system.web.webPages.razor>
Specifically the Version=5.0.0.0 part. Because I'd just re-installed MVC 5.2.x from NuGet, so surely it should be 5.2.0.0?
I checked the Web.config in my ~/Areas/[AreaName]/Views/Web.Config and found that it had everything the same, except it read Version=5.2.0.0:
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
I tried both in both files and found that wherever 5.0.0.0 is used then all intellisense works perfectly for those views.
So, the solution: Modify all Web.config files to use a compatible version for the factoryType. (5.0.0.0 works for me, though 5.2.0.0 does not)
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<!--<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.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>
I'm really unsure about why this is happening though, if anyone has any further information or a better solution to use 5.2.0.0 then please share :)
I once had this problem. This solved my issue:
Open project's output folder in Windows explorer (usually bin folder at the root of the project) and delete everything in it. Then close the solution and reopen it.
Hope that helps.

Visual studio showing syntax errors in razor statements but works

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.
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.
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>

Add my project namespace to MVC web.config

Can I not add my project namespace to the web.config so that the Razor view engine includes my project namespace for all pages in my project? Like so:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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" />
<add namespace = "MyProjectNamespace.NestedNamespace"/>
</namespaces>
</pages>
</system.web.webPages.razor>
It isn't working.
This is an old question, but apparently there is still a bit of a problem with Visual Studio (2015 2017 2019 as of right now). After adding the namespaces to the web.config file in your Views folder, close and re-open all views. The namespaces were not recognized and the views would continuously throw runtime errors until I closed them all.
The namespace needs to be added to the web.config file in your Views folder, not the root project's web.config. This will import the namespace for all cshtml files in that views folder only. If your project has MVC areas, each area will have its own Views folder and a separate views web.config file. To import the namespace into those views, you need to add the namespace to each area's view folder's web.config as well.
You can also add a #using statement to a _ViewStart.cshtml file which will cover all views in the same folder or any subfolders.
#using MyProjectNamespace.NestedNamespace

How to share web.config file between MVC 3 and MVC 4 Project

I have a MVC 3.0 and MVC 4.0 Projects side by side
I did this for the resource sharing between two solutions. it is works fine when i change the config file. changes in details.
In MVC 3.
<assemblies>
<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>
<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>
When i remove the <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> MVC 4 is working fine. after i debug the project in MVC 3.0 it showing an error msg(The type or namespace name 'Ajax' does not exist in the namespace 'System.Web.Mvc' (are you missing an assembly reference?)
) because of i remove the "System.Web.Mvc, Version=3.0.0.0" , if i have to run that means i have to place the refreance again. here i am doing everyting manually, is there is any way to automatic do this ?
For example, two xml files in server side i read that xml file and replace the config file.
Already Tried.
In MVC 4 project i used the WebConfigurationManager to remove the refreance it works , after i opened the MVC 3 it shows that error msg.
You could create a new build configuration for Mvc3 and run a pre-build event that will overwrite your web.config, it's an idea I've adopted from Hanselman.
If you follow the link the only difference for your set up would be that you would have a config called
Web.Config.Mvc3 (and possibly Web.Config.Mvc3Release)
and leave Mvc as your default builds
Web.Config.Debug
Web.Config.Release

Razor Compiler Warning/Errors - ASP.NET MVC 4

I have an issue which seems to have been reported here:
Need razor view engine auto-complete to work in a class library?
My issue is the following Warning:
G:\Accountable\Accountable\Views\LedgerUser\EditorTemplates\LedgerServiceViewModel.cshtml:
ASP.NET runtime error: There is no build provider registered for the
extension '.cshtml'. You can register one in the
section in machine.config or web.config.
Make sure is has a BuildProviderAppliesToAttribute attribute which
includes the value 'Web' or 'All'.
This issue is apparent in all my views. Sample images below.
Now all posts and references online that I have found are referencing to adding various Web.config (as mentioned in the previous post) options, but this doesn't seem to work, as I have these configurations defaulted in the project. Here are my Web Configs:
Main Web.config
View root Web Config
I think this issue maybe while im also getting the following compiler errors on the Views:
The name 'Html' does not exist in the current context
What am I doing wrong?
Arrrgghhh i had missed the following from the ROOT Web.config:
<compilation debug="true" targetFramework="4.0">
<!-- New -->
<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>
This allowed fixed the error and allowed me to use Intellisense within the View.
Putting back the following lines in root Web.config fixed:
<system.web>
<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.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
I got it working by simply unloading and reloading the project. All the namespaces above were already there in the root web.config and I was still getting this issue.
Removing the temporary asp.net framework files works for me.
Stop IIS and remove everything under this folder:
C:\Windows\Microsoft.NET\Framework64\[framework version]\Temporary ASP.NET Files
Then restart IIS. Bam!
I tried by restart IIS, removed Temporary ASP.NET Files and changing the compilation tag in the web config file. None of them work for me.
I fixed the issue by removing and adding System.Web.MVC to the project.
In my case Razor syntax are Highlighted in red.

Resources