--- title: Part 7, Razor Pages with EF Core in ASP.NET Core - Update Related Data author: rick-anderson description: Part 7 of Razor Pages and Entity Framework tutorial series. ms.author: riande ms.date: 07/22/2019 no-loc: [Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] uid: data/ef-rp/update-related-data --- # Part 7, Razor Pages with EF Core in ASP.NET Core - Update Related Data By [Tom Dykstra](https://github.com/tdykstra), [Jon P Smith](https://twitter.com/thereformedprog), and [Rick Anderson](https://twitter.com/RickAndMSFT) [!INCLUDE [about the series](../../includes/RP-EF/intro.md)] ::: moniker range=">= aspnetcore-5.0" This tutorial shows how to update related data. The following illustrations show some of the completed pages. ![Course Edit page](update-related-data/_static/course-edit30.png) ![Instructor Edit page](update-related-data/_static/instructor-edit-courses30.png) ## Update the Course Create and Edit pages The scaffolded code for the Course Create and Edit pages has a Department drop-down list that shows `DepartmentID`, an `int`. The drop-down should show the Department name, so both of these pages need a list of department names. To provide that list, use a base class for the Create and Edit pages. ### Create a base class for Course Create and Edit Create a *Pages/Courses/DepartmentNamePageModel.cs* file with the following code: [!code-csharp[](intro/samples/cu50/Pages/Courses/DepartmentNamePageModel.cs)] The preceding code creates a [SelectList](/dotnet/api/microsoft.aspnetcore.mvc.rendering.selectlist) to contain the list of department names. If `selectedDepartment` is specified, that department is selected in the `SelectList`. The Create and Edit page model classes will derive from `DepartmentNamePageModel`. ### Update the Course Create page model A Course is assigned to a Department. The base class for the Create and Edit pages provides a `SelectList` for selecting the department. The drop-down list that uses the `SelectList` sets the `Course.DepartmentID` foreign key (FK) property. EF Core uses the `Course.DepartmentID` FK to load the `Department` navigation property. ![Create course](update-related-data/_static/ddl30.png) Update *Pages/Courses/Create.cshtml.cs* with the following code: [!code-csharp[](intro/samples/cu50/Pages/Courses/Create.cshtml.cs?highlight=7,18,27-41)] [!INCLUDE[loc comments](~/includes/code-comments-loc.md)] The preceding code: * Derives from `DepartmentNamePageModel`. * Uses to prevent [overposting](xref:data/ef-rp/crud#overposting). * Removes `ViewData["DepartmentID"]`. The `DepartmentNameSL` `SelectList` is a strongly typed model and will be used by the Razor page. Strongly typed models are preferred over weakly typed. For more information, see [Weakly typed data (ViewData and ViewBag)](xref:mvc/views/overview#VD_VB). ### Update the Course Create Razor page Update *Pages/Courses/Create.cshtml* with the following code: [!code-cshtml[](intro/samples/cu50/Pages/Courses/Create.cshtml?highlight=29-34)] The preceding code makes the following changes: * Changes the caption from **DepartmentID** to **Department**. * Replaces `"ViewBag.DepartmentID"` with `DepartmentNameSL` (from the base class). * Adds the "Select Department" option. This change renders "Select Department" in the drop-down when no department has been selected yet, rather than the first department. * Adds a validation message when the department isn't selected. The Razor Page uses the [Select Tag Helper](xref:mvc/views/working-with-forms#the-select-tag-helper): [!code-cshtml[](intro/samples/cu/Pages/Courses/Create.cshtml?range=28-35&highlight=3-6)] Test the Create page. The Create page displays the department name rather than the department ID. ### Update the Course Edit page model Update *Pages/Courses/Edit.cshtml.cs* with the following code: [!code-csharp[](intro/samples/cu50/Pages/Courses/Edit.cshtml.cs?highlight=8,28,35,36,40-66)] The changes are similar to those made in the Create page model. In the preceding code, `PopulateDepartmentsDropDownList` passes in the department ID, which selects that department in the drop-down list. ### Update the Course Edit Razor page Update *Pages/Courses/Edit.cshtml* with the following code: [!code-cshtml[](intro/samples/cu50/Pages/Courses/Edit.cshtml?highlight=17-20,32-35)] The preceding code makes the following changes: * Displays the course ID. Generally the Primary Key (PK) of an entity isn't displayed. PKs are usually meaningless to users. In this case, the PK is the course number. * Changes the caption for the Department drop-down from **DepartmentID** to **Department**. * Replaces `"ViewBag.DepartmentID"` with `DepartmentNameSL`, which is in the base class. The page contains a hidden field (``) for the course number. Adding a `