ASP.NET Core Razor Pages use a page-focused programming model. A page .cshtml and its associated page model .cshtml.cs
In the pages folder, create a new folder called Films and a page called AllFilms.cshtml. Do so via:
This will create a .cshtml and associated page model.cshtml.cs file.
Add a new private readonly variable for the database context.
Create a constructor that uses dependency injection to reference the database context. This removes the need to manage a database context object in this file.
Tip: Use the snippet ctor TAB TAB to build a constructor shell.
Create a List property to hold the films extracted from the database called AllFilms.
When using Entity Framework data can be retrieved with a range of techniques. Here we'll used ToList() method, which is a LINQ Extension Methods.
See how to do this asychronously ↗.
Tip: Use the CTRL . to add the required dependencies in this case EntityFrameWorkCore.
Amend the onGet() handler to remove void as the return type and add async Task. A Task does not return a value and that usually executes asynchronously.
The AllFilms list is now available to the page.
The above means the AllFilms data is available to the view to display. It doesn't have to be added to the ViewData property so it can be accsssed directly as a variable with the @ symbol.
The above using a foreach to loop Films. Each FilmTitle is added to the page.
Amemd the above sample to include other fields.