cerealfordinner
cerealfordinner

Reputation: 23

Blazor/Razor page @model note recognized "The name ''model' does not exist in the current context"

I'm working on a Blazor page which is relatively simple and will pull from a sqlite database that was generated using the Add-Migration command. The tutorial I'm following they have an index.cshtml and thus an index.chtml.cs file where they are making a class IndexModel that inherits from PageModel and then calling that model in the index.cshtml file with the @model IndexModel tag at the top. I'm getting an error that the "The name ''model' does not exist in the current context". I'm relatively new so I apologize if this is something different in my versions of .NET or a difference between Razor and Blazor.

I made the IndexModel class on the same file where my primary class is

public class IndexModel : PageModel
{
    public readonly FlipDbContext _context;

    public IndexModel(FlipDbContext context) => _context = context;

    public async void OnGet()
    {
        Flips = await _context.Flips.ToListAsync();
    }

    public IEnumerable<Flip> Flips { get; set; } = Enumerable.Empty<Flip>();

}

When I add the @model IndexModel to the top of my Index.razor is when I get the issue. Thank you in advance.

Upvotes: 0

Views: 2732

Answers (1)

Yiyi You
Yiyi You

Reputation: 18139

Firstly,Index.razor is a blazor component,not a razor page,so it doesnt't support @model.And Blazor project doesn't contain razor page by default,it only contains blazor components(xxx.razor),you can refer to the official doc.

And since you have the IndexModel associated PageModel,it means the page is a razor page.And if you want to use razor page,you can create a razor page project,here is an official doc.

Upvotes: 2

Related Questions