简体   繁体   中英

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. I am doing a MVC .net core project. I am incorporating Razor in my C# code. 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. 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.

  1. Find and open the file [YourProject]->Models->ErrorViewModel.cs
  2. Copy the namespace name of the 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.

The newly added using directive will looks like this - using [something].Models

  1. Now the build should be successful.

If you have changed the file location of ErrorViewModel.cs

  1. Go to the Views folder then go to the Shared folder and open Error.cshtml
  2. Add changed location from the ErrorViewModel

The reason for this problem is that the ViewModel class is stored in a directory created in the root directory. In this case, the namespace of the newly created ViewModel class is named <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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question error CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?) CS0246: The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference Error CS0246 The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) Error CS0246: The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?) error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) CS0246: The type or namespace name 'Employee' could not be found (are you missing a using directive or an assembly reference?)? error CS0246: The type or namespace name 'BannerPosition' could not be found are you missing a using directive or an assembly reference? Error CS0246 The type or namespace name 'CreateRandomAnswersForKey' could not be found (are you missing a using directive or an assembly reference?)? CS0246 The type or namespace name 'MailKit' could not be found (are you missing a using directive or an assembly reference? 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