简体   繁体   English

生成的Razor视图文件无法识别模型类

[英]Generated Razor view file does not recognise the model class

As I build and run ( dotnet run ) my ASP.NET Core MVC project using VS Code, I get the following two error messages: 当我使用VS Code构建和运行( dotnet run )我的ASP.NET Core MVC项目时,收到以下两条错误消息:

obj/Debug/netcoreapp2.1/Razor/Views/Product/List.g.cshtml.cs(28,88): error CS0246: The type or namespace name 'ProductsListViewModel' could not be found (are you missing a using directive or an assembly reference?) [/Users/pedram/OneDrive - House of Friends AB/visualstudioonmacprojects/SportsStore/SportsStore.csproj] obj / Debug / netcoreapp2.1 / Razor / Views / Product / List.g.cshtml.cs(28,88):错误CS0246:找不到类型或名称空间名称“ ProductsListViewModel”(您是否缺少using指令或程序集引用?)[/ Users / pedram / OneDrive-友议院AB / visualstudioonmacprojects / SportsStore / SportsStore.csproj]

obj/Debug/netcoreapp2.1/Razor/Views/Product/List.g.cshtml.cs(128,71): error CS0246: The type or namespace name 'ProductsListViewModel' could not be found (are you missing a using directive or an assembly reference?) [/Users/pedram/OneDrive - House of Friends AB/visualstudioonmacprojects/SportsStore/SportsStore.csproj] obj / Debug / netcoreapp2.1 / Razor / Views / Product / List.g.cshtml.cs(128,71):错误CS0246:找不到类型或名称空间名称“ ProductsListViewModel”(您是否缺少using指令或程序集引用?)[/ Users / pedram / OneDrive-友议院AB / visualstudioonmacprojects / SportsStore / SportsStore.csproj]

The build failed. 构建失败。 Please fix the build errors and run again. 请修复构建错误,然后重新运行。

When I navigate to the files mentioned I realize that the class ProductsListViewModel needs to be addressed using its full name SportsStore.Models.ViewModels.ProductsListViewModel in both places for the project to build correctly. 当我导航到提到的文件时,我意识到SportsStore.Models.ViewModels.ProductsListViewModel在两个地方使用其全名SportsStore.Models.ViewModels.ProductsListViewModel来解决类ProductsListViewModel ,才能正确构建项目。 But this only resolves the problem until the next build. 但这只能解决问题,直到下一个版本。

Cleaning the project before building ( dotnet clean ) does not seem to help either. 在构建之前清理项目( dotnet clean )似乎也无济于事。

I do not have much control over things happening inside the obj folder. 我对obj文件夹中发生的事情没有太多控制权。 What is causing this missing namespace problem? 是什么导致此缺少名称空间的问题?

Update: 更新:

The contents of /Views/Product/List.cshtml /Views/Product/List.cshtml的内容

@model ProductsListViewModel
@foreach (var p in Model.Products) {
    @Html.Partial("ProductSummary", p)
}
<div page-model="@Model.PagingInfo" page-action="List" page-classes-enabled="true"
     page-class="btn" page-class-normal="btn-secondary"
     page-class-selected="btn-primary" class="btn-group pull-right m-1">
</div>

In the first line of your .cshtml file, you declare the type of the model that is being used in your view. 在.cshtml文件的第一行中,您声明在视图中使用的模型的类型。 From the error message, it's clear that ProductsListViewModel cannot be resolved. 从错误消息中很明显,无法解析ProductsListViewModel To solve that issue, you have at least two options: 要解决该问题,您至少有两个选择:

  1. Use @model SportsStore.Models.ViewModels.ProductsListViewModel , which is a fully-qualified name (FQN). 使用@model SportsStore.Models.ViewModels.ProductsListViewModel ,它是完全限定的名称(FQN)。
  2. Use @using , like this: 使用@using ,像这样:

     @using SportsStore.Models.ViewModels @model ProductsListViewModel 

It's mostly a matter of preference as to which you should choose. 在选择哪种方式时, 主要取决于您的偏好。

Visual Studio Code doesn't seem to support Razor files as well as Visual Studio - It seems to have decent syntax-highlighting support, but doesn't seem to compile the .cshtml files in order to discover compilation errors like the one you're having. Visual Studio Code似乎不像Visual Studio一样支持Razor文件-它似乎具有不错的语法突出显示支持,但似乎不编译.cshtml文件以发现像您这样的编译错误。有。

The file you first modified ( List.g.cshtml.cs ) is a generated file, that is produced from processing your .cshtml file and should not be modified. 您首先修改的文件( List.g.cshtml.cs )是一个生成的文件,是通过处理.cshtml文件生成的,因此不应进行修改。 If you do modify it, it gets replaced next time the source .cshtml file is processed. 如果您确实对其进行了修改,则下次处理源.cshtml文件时,它将被替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM