Home > OS >  Class does not include a variable which it does
Class does not include a variable which it does

Time:09-19

I've been making a webapp in Blazor and have been stuck on this error, which I cannot find any documentation of.

I wrote in my razor page:

    @foreach (App app in AppService.AllApps)
    {
        <tr>
            <td>@app.Title</td>
            <td>@app.Price</td>
        </tr>
    }

And the Visual Studio error says "'App' does not contain a definition for 'Title' and no accessible extension method 'Title' accepting a first argument of type 'App' could be found." (The same for Price)

So I looked at my class, "App", and it contained:

        public string Title { get; set; }

So, I don't understand why it didn't recognize that.

Thanks, in advance, any advice is helpful.

CodePudding user response:

When you look in your Project files (Solution Explorer) you will see there is a file App.razor.

So your project has two different App classes, and that confuses both you and the compiler. In this case it preferred he App component.

The best approach is to rename your own App class to eg Application.

Option two is to use a qualified name, like @foreach (MyClasses.App app in ...) . I took a guess at the namespace here.

And option three is to rename the App.razor file but be aware it is used in a few different places, like Program.cs and _Host.razor / index.cshtml .

  • Related