In my ASP.NET MVC 5 Application I have all my models in the namespace MyApp.Models. To use the model inside my view, e.g. to tell the view which model he should use with #model UserLoginModel, I have to import the namespace with using MyApp.Models.
Thats not a big deal but I would prefer it to include this namespace automatically in every of my views to avoid redundant importing in every view. I found out that the web.config file in my views-folder seems to be right for this job
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Optimization"/>
<add namespace="MyApp" />
<add namespace="MyApp.Models"/>
</namespaces>
</pages>
But its not working. All of this was pre-generated by Visual Studio. I only added the <add namespace="MyApp.Models"/> for my models.
On SO I found a similar question like my but the asker only say that it should work by using the Views/web.config file as I tried and the answears follow his question which is specified to the new beta of ASP.NET MVC 6. But I'm using MVC 5.1.2 and cant get make this work, he can't find the view without including the namespace with #using in the view.
Related
I am using Visual Studio 2013 Web Express. In my ASP.NET MVC application view I cannot call or even see the #Html.EditorFor method. The intellisense never pops it up.
Any ideas?
EDITED: Same goes with #Html.BeginForm. The method never pops up!
It sounds like the namespace is not available.
In the first instance, try adding
#using System.Web.Mvc.Html
at the top of your view to see if that helps. If it does, then there is probably a problem with your View specific Web.config. In the root of your Views folder there should be a Web.config file. Note that if you use Areas then there should be one of those in each Views folder.
Inside that Web.config there should be a section like the following:
<configuration>
...other stuff...
<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" />
<add namespace="WebApplication1" />
</namespaces>
</pages>
</system.web.webPages.razor>
...other stuff...
</configuration>
Essentially that section is the equivalent of adding using statements inside each view.
After updating my build via an SVN repo, all of a sudden all my view code is broken because my Views can't seem to resolve the System.Web.Mvc namespace. I keep getting the above message with reference to Html methods (e.g. Html.DisplayFor, Html.RadioButton, etc.in Mvc views. However, the namespace is clearly included in the Web.config
<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.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
Any other ideas as to why this might not work?
Just a shot in the dark, but I would check the reference path for System.Web.Mvc. Perhaps you are referencing some version of the System.Web.Mvc library that no longer exists in the directory location on your machine since u pulled down a fresh copy of the repo.
I created an Asp.NET MVC 3 web application. However, when I try to deploy it I get this error.
'ModelType' is not declared. It may be inaccessible due to its protection level.
on the second line of the code where I declare my model type. It works great locally where MVC 3 is installed but not on the server where it isn't. I've included all the dll's that it normally requires to run without the installation. Also this code works fine on another server where MVC 3 isn't installed so I'm not sure what the problem is.
here is the beginning of the code:
#Imports System.Data.SqlClient
#ModelType SqlDataReader
#Code
Layout = Nothing
End Code
it fails on the second line
This would happen if the MvcWebPageRazorHost wasn't registered.
Make sure that you have the configuration in your ~/Views/Web.config on that server:
<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>
Make sure the Web.config file in the \Views\ folder still exists. I accidentally deleted mine which caused the error "#ModelType is not declared. It may be inaccessible..."
I downloaded MVC2 Futures and referenced it to my current MVC2 Project. However, if I want to call an HtmlHelper from MVC2 Futures I need to <%# Import Namespace="Microsoft.Web.Mvc" %>
So I decided to add it on my Web.Config:
<pages>
<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="Microsoft.Web.Mvc" />
<add namespace="Xpedite.Mvc.Html" />
</namespaces>
</pages>
but, this caused me errors. Could this assemblies co-exist? If yes, How?
I'm pretty sure you need the MVC3 Futures pack:
http://aspnet.codeplex.com/releases/view/54306
The MVC2 Future was made to work against MVC1. I had the same issue and upgrading to the next futures release solved it for me.
I am developing a Web app that is based on ASP.NET 3.5, and i added some page from ASP.NET MVC, everything is ok, until i use the "HTML" helper class, then the page is not being able be render, because this is not recognized.
For Example : Html.BeginForm() => this is not recognize as a method
But if i used "System.Web.Mvc.Html.FormExtensions.BeginForm", which is a extension method itself, it work fine.
Environment : ASP.NET MVC 2 and ASP.NET 3.5
Anyone experiencing this problem?
Thanks.
Do you have the System.Web.Mvc.Html namespace included in web.config's page assemblies list? I.e.:
<pages>
<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>