Reputation: 7278
As I build and run (dotnet run
) my ASP.NET Core MVC project using VS Code, I get the following two error messages:
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(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]
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. But this only resolves the problem until the next build.
Cleaning the project before building (dotnet clean
) does not seem to help either.
I do not have much control over things happening inside the obj folder. What is causing this missing namespace problem?
Update:
The contents of /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>
Upvotes: 1
Views: 3974
Reputation: 93003
In the first line of your .cshtml file, you declare the type of the model that is being used in your view. From the error message, it's clear that ProductsListViewModel
cannot be resolved. To solve that issue, you have at least two options:
@model SportsStore.Models.ViewModels.ProductsListViewModel
, which is a fully-qualified name (FQN).Use @using
, like this:
@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.
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. If you do modify it, it gets replaced next time the source .cshtml file is processed.
Upvotes: 6