asp.net mvc 3 'ModelType' is not declared

2020-03-31 02:58发布

问题:

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

回答1:

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>


回答2:

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