--- title: Razor Pages with EF Core in ASP.NET Core - Update Related Data - 7 of 8 author: rick-anderson description: In this tutorial you'll update related data by updating foreign key fields and navigation properties. manager: wpickett ms.author: riande ms.date: 11/15/2017 ms.prod: asp.net-core ms.technology: aspnet ms.topic: get-started-article uid: data/ef-rp/update-related-data --- # Razor Pages with EF Core in ASP.NET Core - Update Related Data - 7 of 8 By [Tom Dykstra](https://github.com/tdykstra), and [Rick Anderson](https://twitter.com/RickAndMSFT) [!INCLUDE[about the series](../../includes/RP-EF/intro.md)] This tutorial demonstrates updating related data. If you run into problems you can't solve, download the [completed app for this stage](https://github.com/aspnet/Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/StageSnapShots/cu-part7). The following illustrations shows some of the completed pages. ![Course Edit page](update-related-data/_static/course-edit.png) ![Instructor Edit page](update-related-data/_static/instructor-edit-courses.png) Examine and test the Create and Edit course pages. Create a new course. The department is selected by its primary key (an integer), not its name. Edit the new course. When you have finished testing, delete the new course. ## Create a base class to share common code The Courses/Create and Courses/Edit pages each need a list of department names. Create the *Pages/Courses/DepartmentNamePageModel.cshtml.cs* base class for the Create and Edit pages: [!code-csharp[](intro/samples/cu/Pages/Courses/DepartmentNamePageModel.cshtml.cs?highlight=9,11,20-21)] The preceding code creates a [SelectList](https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.rendering.selectlist?view=aspnetcore-2.0) 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`. ## Customize the Courses Pages When a new course entity is created, it must have a relationship to an existing department. To add a department while creating a course, the base class for Create and Edit contains a drop-down list for selecting the department. The drop-down list 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/ddl.png) Update the Create page model with the following code: [!code-csharp[](intro/samples/cu/Pages/Courses/Create.cshtml.cs?highlight=7,18,32-999)] The preceding code: * Derives from `DepartmentNamePageModel`. * Uses `TryUpdateModelAsync` to prevent [overposting](xref:data/ef-rp/crud#overposting). * Replaces `ViewData["DepartmentID"]` with `DepartmentNameSL` (from the base class). `ViewData["DepartmentID"]` is replaced with the strongly typed `DepartmentNameSL`. 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 Courses Create page Update *Pages/Courses/Create.cshtml* with the following markup: [!code-cshtml[](intro/samples/cu/Pages/Courses/Create.cshtml?highlight=29-34)] The preceding markup 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" 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 Courses Edit page. Update the edit page model with the following code: [!code-csharp[](intro/samples/cu/Pages/Courses/Edit.cshtml.cs?highlight=8,28,35,36,40,47-999)] The changes are similar to those made in the Create page model. In the preceding code, `PopulateDepartmentsDropDownList` passes in the department ID, which select the department specified in the drop-down list. Update *Pages/Courses/Edit.cshtml* with the following markup: [!code-cshtml[](intro/samples/cu/Pages/Courses/Edit.cshtml?highlight=17-20,32-35)] The preceding markup 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 from **DepartmentID** to **Department**. * Replaces `"ViewBag.DepartmentID"` with `DepartmentNameSL` (from the base class). * Adds the "Select Department" option. This change renders "Select Department" rather than the first department. * Adds a validation message when the department isn't selected. The page contains a hidden field (``) for the course number. Adding a `