Visual Studio代码中MVC 6无法识别ViewModel

本文关键字:识别 ViewModel MVC Studio 代码 Visual | 更新日期: 2024-05-25 15:45:34

我创建了一个名为AddNewInventoryViewModel的ViewModel(Project是ASP.NET MVC,在OSX上使用Visual Studio Code,运行时为.NET Core 5.0),并将其添加到ViewModel models文件夹中,这样我就有了以下结构:

ViewModels/Home/AddNewInventoryViewModel.cs
Views/Home/AddNewInventory.cshtml

我的观点是这样的:

@model AddNewInventoryViewModel
...Content here

我的家庭控制器操作方法看起来像这样:

[HttpGet]
public IActionResult AddNewInventory()
{
    return View();    
}

为了简洁起见,我不包括ViewModel.cs文件本身。

然而,当我访问我的应用程序(AddNewInventory.cshtml)中的页面时,我收到了错误:

DNXCore,Version=v5.0 error CS0246: The type or namespace 
'AddNewInventoryViewModel' could not be found (are you missing a using directive or an assembly reference?)

为什么会出现错误?我错过了什么?

Visual Studio代码中MVC 6无法识别ViewModel

出现此错误的原因是未能在Views文件夹的_ViewImports.cshtml中添加引用。例如,下面我错过了这个项目。ViewModels。主页参考。

@using project
@using project.Models
@using project.ViewModels.Account
@using project.ViewModels.Manage
@using project.ViewModels.Home
@using Microsoft.AspNet.Identity
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

我补充说,一切都很好。