Razor View throwing “The name 'model' does

2019-01-06 10:55发布

After significant refactoring in my MVC 4 application, and Razor shows this error while debugging Views:

The name 'model' does not exist in the current context.

This is the offending line of code:

@model ICollection<DataSourceByActive>

I know that the usage of @model is correct.

Why is this happening? How can I fix it?

20条回答
手持菜刀,她持情操
2楼-- · 2019-01-06 11:33

Changing following line in web.config of view folder solved the same error.

From

 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

To

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
查看更多
\"骚年 ilove
3楼-- · 2019-01-06 11:34

In my case, I removed web.config file from Views folder by accident. I added it back , and it was OK.

查看更多
何必那么认真
4楼-- · 2019-01-06 11:37

make sure you have the following in both your site web.config and views directory web.config in the appSettings section

<add key="webpages:Version" value="2.0.0.0" />

For MVC5 use:

<add key="webpages:Version" value="3.0.0.0" />

(And it only exists in the main Web.config file.)

查看更多
The star\"
5楼-- · 2019-01-06 11:37

I was trying to add a view which were outside of my "Views" folder (just to organize my code differently, I guess), when I had this issue. Creating the view inside Views (as by convention) solved it.

查看更多
The star\"
6楼-- · 2019-01-06 11:38

For some reason my web.config had 0.0.0.0 in the oldVersion attribute:

<runtime>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
</runtime>

changing to 1.0.0.0 was the solution:

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
  </dependentAssembly>
查看更多
手持菜刀,她持情操
7楼-- · 2019-01-06 11:42

I think you have messed up the web.config file which lives in the Views folder.

Create a new project targeting the same .NET framework and copy its Views/web.config file on top of the one in your current project. This will fix your problem.

Also, as Dudeman3000 commented, if you have Areas in your MVC project they all have Views\web.config files too.

查看更多
登录 后发表回答