简体   繁体   English

CS0246 找不到类型或命名空间名称“ErrorViewModel”(您是否缺少 using 指令或程序集引用?)

[英]CS0246 The type or namespace name 'ErrorViewModel' could not be found (are you missing a using directive or an assembly reference?)

I am getting a CS0246 error code.我收到一个 CS0246 错误代码。 I am doing a MVC .net core project.我正在做一个 MVC .net 核心项目。 I am incorporating Razor in my C# code.我将 Razor 合并到我的 C# 代码中。 I received this error having doing a build.我在进行构建时收到此错误。 I am getting an error on the last line.我在最后一行收到错误。 Could anyone help me figure this out?谁能帮我解决这个问题?

    [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"d6a5625cc8fb4476f348b0fe9041c550465d8bf9", @"/Views/Shared/Error.cshtml")]
    [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"53c99bf587b2b24ba6d4f1516a026a5e81271c09", @"/Views/_ViewImports.cshtml")]
    public class Views_Shared_Error : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<ErrorViewModel>

In Error.cshtml page add @model libraryname.ErrorViewModel at the top of the page instead of just ErrorViewModel.在 Error.cshtml 页面中,在页面顶部添加 @model libraryname.ErrorViewModel 而不仅仅是 ErrorViewModel。 Then rebuild the project and see if it works.然后重建项目,看看它是否有效。

this error usually occurs when you edit the file placement and unable to direct the file.In my case _LoginPartial.cshtml was outside the shared folder.It usually occurs if file moved to the wrong folder so the model can't specify the direction.此错误通常发生在您编辑文件位置并且无法定向文件时。在我的情况下,_LoginPartial.cshtml 位于共享文件夹之外。如果文件移动到错误的文件夹通常会发生这种情况,因此 model 无法指定方向。

  1. Find and open the file [YourProject]->Models->ErrorViewModel.cs找到并打开文件 [YourProject]->Models->ErrorViewModel.cs
  2. Copy the namespace name of the ErrorViewModel class.复制 ErrorViewModel class 的命名空间名称。
  3. Add a new using directive just couple of lines above of the line having the error you reported and paste the copied namespace value.在您报告的错误行的上方几行添加一个新的 using 指令,然后粘贴复制的命名空间值。

The newly added using directive will looks like this - using [something].Models新添加的 using 指令将如下所示 - using [something].Models

  1. Now the build should be successful.现在构建应该成功了。

If you have changed the file location of ErrorViewModel.cs如果您更改了 ErrorViewModel.cs 的文件位置

  1. Go to the Views folder then go to the Shared folder and open Error.cshtml Go 到 Views 文件夹,然后 go 到 Shared 文件夹并打开 Error.cshtml
  2. Add changed location from the ErrorViewModel从 ErrorViewModel 添加更改的位置

The reason for this problem is that the ViewModel class is stored in a directory created in the root directory.出现此问题的原因是ViewModel class 存储在根目录中创建的目录中。 In this case, the namespace of the newly created ViewModel class is named <root-namespace>.<folder-name> .在这种情况下,新创建的ViewModel class 的命名namespace被命名为<root-namespace>.<folder-name> To avoid this situation, using the following will result in an error:为避免这种情况,使用以下将导致错误:

@model <folder-name>.<view-model-name>

The way to avoid this is to type the full namespace :避免这种情况的方法是输入完整的namespace

@model <full-namespace>.<view-model-name>

暂无
暂无

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

相关问题 错误 CS0246:找不到类型或命名空间名称“Npgsql”(您是否缺少 using 指令或程序集引用?) - error CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?) CS0246:找不到类型或命名空间名称&#39;MySql&#39;(您是否缺少using指令或程序集引用 - CS0246: The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference 错误CS0246找不到类型或名称空间名称“ Windows”(是否缺少using指令或程序集引用?) - Error CS0246 The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“StreamingContext”(是否缺少 using 指令或程序集引用?) - Error CS0246: The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“IWebHostEnvironment”(您是否缺少 using 指令或程序集引用?) - error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) CS0246:找不到类型或名称空间名称“ Employee”(您是否缺少using指令或程序集引用?)? - CS0246: The type or namespace name 'Employee' could not be found (are you missing a using directive or an assembly reference?)? 错误 CS0246:找不到类型或命名空间名称“BannerPosition”是否缺少 using 指令或程序集引用? - error CS0246: The type or namespace name 'BannerPosition' could not be found are you missing a using directive or an assembly reference? 错误 CS0246 找不到类型或命名空间名称“CreateRandomAnswersForKey”(您是否缺少 using 指令或程序集引用?)? - Error CS0246 The type or namespace name 'CreateRandomAnswersForKey' could not be found (are you missing a using directive or an assembly reference?)? CS0246 找不到类型或命名空间名称“MailKit”(是否缺少 using 指令或程序集引用? - CS0246 The type or namespace name 'MailKit' could not be found (are you missing a using directive or an assembly reference? C# 错误 CS0246 找不到类型或命名空间名称“Socket”(您是否缺少 using 指令或程序集引用) - C# error CS0246 The type or namespace name 'Socket' could not be found (are you missing a using directive or an assembly reference)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM