Home > Software design >  Why am i getting this CS0246 errors? Razor Page
Why am i getting this CS0246 errors? Razor Page

Time:02-03

enter image description here

#pragma checksum "C:\Users\Nathan\Documents\ASPNET PJ\Views\Contato\Criar.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0419d52b5c6c647b8ba18e9d7de1b5045cdb5bb3"
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.Views_Contato_Criar), @"mvc.1.0.view", @"/Views/Contato/Criar.cshtml")]
namespace AspNetCore
{
    #line hidden
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Mvc.ViewFeatures;
    using ContatoModel;
#nullable restore
#line 1 "C:\Users\Nathan\Documents\ASPNET PJ\Views\_ViewImports.cshtml"
using ASPNET_PJ;

#line default
#line hidden
#nullable disable
#nullable restore
#line 2 "C:\Users\Nathan\Documents\ASPNET PJ\Views\_ViewImports.cshtml"
using ASPNET_PJ.Models;

#line default
#line hidden
#nullable disable
    [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"0419d52b5c6c647b8ba18e9d7de1b5045cdb5bb3", @"/Views/Contato/Criar.cshtml")]
    [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"30bddb7a998eb7023ecaaf3235c8530011feaaed", @"/Views/_ViewImports.cshtml")]
    #nullable restore
    public class Views_Contato_Criar : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<ContatoModel>
    #nullable disable
   

Why am i getting these errors? I've already try to use "using Contatomodel;" but still dont working

To build and run without any of these errors

CodePudding user response:

'using' is for namespaces, not classes. It is to tell EF where to find class references.

You are defining a Razor view typed for a ContatoModel...

public class Views_Contato_Criar : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<ContatoModel>

ASP.Net will likely not know where to locate this Model since it is most likely not in the same namespace (think Folder) as the view, so you need to use the using to tell it where to search for class references.

Go to your ContatoModel class file and you should see it is within a namespace scope. Replace the using ContatoModel; with a using and that namespace. For example, if we had a /Models folder in our application (called MyApplication) the default namespace would be like below:

namespace MyApplication.Models
{
    public class ContantoModel
    {
        // ...
    }
}

You would want to use:

using MyApplication.Models;

... in order for the View to resolve where to find the Model. You can usually figure out the namespace by looking at the folder structure, but that is just the default naming. As classes get moved around often their namespace doesn't reflect their new location so it's best to always check the class namespace itself.

This is fairly fundamental C# to understand so I would highly recommend reading and practicing on C# classes, namespaces, object references, etc. before diving head-long into things like Razor views where things can get a little fast and loose to bring C# into the land of HTML.

  • Related