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条回答
We Are One
2楼-- · 2019-01-06 11:25

None of the existing answers worked for me, but I found what did work for me by comparing the .csproj files of different projects. The following manual edit to the .csproj XML-file solved the Razor-intellisense problem for me, maybe this can help someone else who has tried all the other answers to no avail. Key is to remove any instances of <Private>False</Private> in the <Reference>'s:

<ItemGroup>
  <Reference Include="Foo">
    <HintPath>path\to\Foo</HintPath>
    <!-- <Private>False</Private> -->
  </Reference>
  <Reference Include="Bar">
    <HintPath>path\to\Bar</HintPath>
    <!-- <Private>True</Private> -->
  </Reference>
</ItemGroup>

I don't know how those got there or exactly what they do, maybe someone smarter than me can add that information. I was just happy to finally solve this problem.

查看更多
在下西门庆
3楼-- · 2019-01-06 11:26

In my case, the issue was that after upgrading the project from MVC 4 to MVC 5 I somehow missed a version change in the Views/web.config:

    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">            

It still had the old 2.0.0.0 version. After changing the version to 3.0.0.0 everything started working just right.

Also, because of this problem, Visual Studio 2015 Community Edition would start bashing the CPU (30-40% usage at idle) every time I would open a .cshtml file.

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-06 11:29

In my case, I recently updated from MVC 4 to MVC 5, which screws up the web.config pretty badly. This article helped tremendously.

http://www.asp.net/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

The bottom line is that you need to check all your version number references in your web.config and Views/web.config to make sure that they are referencing the correct upgraded versions associated with MVC 5.

查看更多
疯言疯语
5楼-- · 2019-01-06 11:30

I had the same issue, I created a new project and copied the web.config files as recommended in the answer by Gupta, but that didn't fix things for me. I checked answer by Alex and Liam, I thought this line must have been copied from the new web.config, but it looks like the new project itself didn't have this line (MVC5):

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

Adding the line to the views/web.config file solved the issue for me.

查看更多
乱世女痞
6楼-- · 2019-01-06 11:30

Changing to @Model from @model did the job for me.

@model represents the View Model object type. @Model represents the View Model object.

查看更多
祖国的老花朵
7楼-- · 2019-01-06 11:33

Here is what I did:

  1. Close Visual Studio
  2. Delete the SUO file
  3. Restart Visual Studio

The .suo file is a hidden file in the same folder as the .svn solution file and contains the Visual Studio User Options.

查看更多
登录 后发表回答