diff --git a/aspnetcore/data/ef-rp/intro/samples/cu-completed-stages/part7/ContosoUniversity/Pages/Courses/Create.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu-completed-stages/part7/ContosoUniversity/Pages/Courses/Create.cshtml.cs index 0e8927d5728e..b3eb5e393a51 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu-completed-stages/part7/ContosoUniversity/Pages/Courses/Create.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu-completed-stages/part7/ContosoUniversity/Pages/Courses/Create.cshtml.cs @@ -43,7 +43,7 @@ public async Task OnPostAsync() return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. emptyCourse.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu-completed-stages/part7/ContosoUniversity/Pages/Courses/Edit.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu-completed-stages/part7/ContosoUniversity/Pages/Courses/Edit.cshtml.cs index fa5ba61efb7e..c4ccad53760c 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu-completed-stages/part7/ContosoUniversity/Pages/Courses/Edit.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu-completed-stages/part7/ContosoUniversity/Pages/Courses/Edit.cshtml.cs @@ -32,7 +32,7 @@ public async Task OnGetAsync(int? id) return NotFound(); } - // Select current DepartmentID. + // Populate departments dropdown. Course.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, Course.DepartmentID); return Page(); } @@ -60,7 +60,7 @@ public async Task OnPostAsync(int? id) return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. courseToUpdate.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Courses/Create.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Courses/Create.cshtml.cs index 610cbdf2ac28..8ab53562dc6f 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Courses/Create.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Courses/Create.cshtml.cs @@ -41,7 +41,7 @@ public async Task OnPostAsync() return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. emptyCourse.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Courses/Edit.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Courses/Edit.cshtml.cs index c3fb527d311a..825eec2db7a7 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Courses/Edit.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Courses/Edit.cshtml.cs @@ -32,7 +32,7 @@ public async Task OnGetAsync(int? id) return NotFound(); } - // Select current DepartmentID. + // Populate departments dropdown. Course.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context,Course.DepartmentID); return Page(); } @@ -55,7 +55,7 @@ public async Task OnPostAsync(int? id) return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. courseToUpdate.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part7/Pages/Courses/Create.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part7/Pages/Courses/Create.cshtml.cs index a4b96567b041..4c412b441bf9 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part7/Pages/Courses/Create.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part7/Pages/Courses/Create.cshtml.cs @@ -53,7 +53,7 @@ public async Task OnPostAsync() return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. emptyCourse.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part7/Pages/Courses/Edit.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part7/Pages/Courses/Edit.cshtml.cs index 709aeac42b71..eee8962bed82 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part7/Pages/Courses/Edit.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part7/Pages/Courses/Edit.cshtml.cs @@ -38,7 +38,7 @@ public async Task OnGetAsync(int? id) return NotFound(); } - // Select current DepartmentID. + // Populate departments dropdown. Course.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, Course.DepartmentID); return Page(); } @@ -61,7 +61,7 @@ public async Task OnPostAsync(int? id) return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. courseToUpdate.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part8/Pages/Courses/Create.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part8/Pages/Courses/Create.cshtml.cs index a4b96567b041..4c412b441bf9 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part8/Pages/Courses/Create.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part8/Pages/Courses/Create.cshtml.cs @@ -53,7 +53,7 @@ public async Task OnPostAsync() return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. emptyCourse.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part8/Pages/Courses/Edit.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part8/Pages/Courses/Edit.cshtml.cs index 709aeac42b71..eee8962bed82 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part8/Pages/Courses/Edit.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu20snapshots/cu-part8/Pages/Courses/Edit.cshtml.cs @@ -38,7 +38,7 @@ public async Task OnGetAsync(int? id) return NotFound(); } - // Select current DepartmentID. + // Populate departments dropdown. Course.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, Course.DepartmentID); return Page(); } @@ -61,7 +61,7 @@ public async Task OnPostAsync(int? id) return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. courseToUpdate.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu30/Pages/Courses/Create.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu30/Pages/Courses/Create.cshtml.cs index 2d9006b15167..258ec1968602 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu30/Pages/Courses/Create.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu30/Pages/Courses/Create.cshtml.cs @@ -36,7 +36,7 @@ public async Task OnPostAsync() return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. emptyCourse.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu30/Pages/Courses/Edit.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu30/Pages/Courses/Edit.cshtml.cs index 582ff5a656da..f23a02ab60eb 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu30/Pages/Courses/Edit.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu30/Pages/Courses/Edit.cshtml.cs @@ -32,7 +32,7 @@ public async Task OnGetAsync(int? id) return NotFound(); } - // Select current DepartmentID. + // Populate departments dropdown. Course.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, Course.DepartmentID); return Page(); } @@ -60,7 +60,7 @@ public async Task OnPostAsync(int? id) return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. courseToUpdate.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu50/Pages/Courses/Create.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu50/Pages/Courses/Create.cshtml.cs index 2d9006b15167..258ec1968602 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu50/Pages/Courses/Create.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu50/Pages/Courses/Create.cshtml.cs @@ -36,7 +36,7 @@ public async Task OnPostAsync() return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. emptyCourse.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/intro/samples/cu50/Pages/Courses/Edit.cshtml.cs b/aspnetcore/data/ef-rp/intro/samples/cu50/Pages/Courses/Edit.cshtml.cs index 582ff5a656da..f23a02ab60eb 100644 --- a/aspnetcore/data/ef-rp/intro/samples/cu50/Pages/Courses/Edit.cshtml.cs +++ b/aspnetcore/data/ef-rp/intro/samples/cu50/Pages/Courses/Edit.cshtml.cs @@ -32,7 +32,7 @@ public async Task OnGetAsync(int? id) return NotFound(); } - // Select current DepartmentID. + // Populate departments dropdown. Course.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, Course.DepartmentID); return Page(); } @@ -60,7 +60,7 @@ public async Task OnPostAsync(int? id) return RedirectToPage("./Index"); } - // Select DepartmentID if TryUpdateModelAsync fails. + // Repopulate departments dropdown. courseToUpdate.DepartmentID determines the selected item. PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return Page(); } diff --git a/aspnetcore/data/ef-rp/update-related-data.md b/aspnetcore/data/ef-rp/update-related-data.md index 04f11200b2cd..7bbb093802c8 100644 --- a/aspnetcore/data/ef-rp/update-related-data.md +++ b/aspnetcore/data/ef-rp/update-related-data.md @@ -30,8 +30,10 @@ 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 to contain the list of department names. If `selectedDepartment` is specified, that department is selected in the `SelectList`. +The preceding code creates a to contain the list of department names. The `selectedDepartment` parameter allows the calling code to specify the item that will be selected when the drop-down list is rendered. However, when using the Select Tag Helper with `asp-for`, the selected item is determined by the model property value (such as `Course.DepartmentID`), not by the `selectedDepartment` parameter. +> [!IMPORTANT] +> When using the Select Tag Helper with `asp-for="Course.DepartmentID"`, the selected option is automatically determined by the value of `Course.DepartmentID`. The `selectedDepartment` parameter passed to the SelectList constructor is ignored in this scenario. The Create and Edit page model classes will derive from `DepartmentNamePageModel`. ### Update the Course Create page model @@ -77,7 +79,7 @@ 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. +The changes are similar to those made in the Create page model. In the preceding code, `PopulateDepartmentsDropDownList` passes in the department ID. When using the Select Tag Helper with `asp-for="Course.DepartmentID"`, the selected item in the drop-down list is determined by the value of `Course.DepartmentID`, not by the `selectedDepartment` parameter passed to the SelectList constructor. ### Update the Course Edit Razor page @@ -253,7 +255,7 @@ Create a `Pages/Courses/DepartmentNamePageModel.cs` file with the following code [!code-csharp[](intro/samples/cu30/Pages/Courses/DepartmentNamePageModel.cs)] -The preceding code creates a to contain the list of department names. If `selectedDepartment` is specified, that department is selected in the `SelectList`. +The preceding code creates a to contain the list of department names. The `selectedDepartment` parameter allows the calling code to specify the item that will be selected when the drop-down list is rendered. However, when using the Select Tag Helper with `asp-for`, the selected item is determined by the model property value (such as `Course.DepartmentID`), not by the `selectedDepartment` parameter. The Create and Edit page model classes will derive from `DepartmentNamePageModel`. @@ -300,7 +302,7 @@ Update `Pages/Courses/Edit.cshtml.cs` with the following code: [!code-csharp[](intro/samples/cu30/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. +The changes are similar to those made in the Create page model. In the preceding code, `PopulateDepartmentsDropDownList` passes in the department ID. When using the Select Tag Helper with `asp-for="Course.DepartmentID"`, the selected item in the drop-down list is determined by the value of `Course.DepartmentID`, not by the `selectedDepartment` parameter passed to the SelectList constructor. ### Update the Course Edit Razor page @@ -470,7 +472,7 @@ The Courses/Create and Courses/Edit pages each need a list of department names. [!code-csharp[](intro/samples/cu/Pages/Courses/DepartmentNamePageModel.cshtml.cs?highlight=9,11,20-21)] -The preceding code creates a to contain the list of department names. If `selectedDepartment` is specified, that department is selected in the `SelectList`. +The preceding code creates a to contain the list of department names. The `selectedDepartment` parameter allows the calling code to specify the item that will be selected when the drop-down list is rendered. However, when using the Select Tag Helper with `asp-for`, the selected item is determined by the model property value (such as `Course.DepartmentID`), not by the `selectedDepartment` parameter. The Create and Edit page model classes will derive from `DepartmentNamePageModel`. @@ -517,7 +519,7 @@ Replace the code in `Pages/Courses/Edit.cshtml.cs` 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. +The changes are similar to those made in the Create page model. In the preceding code, `PopulateDepartmentsDropDownList` passes in the department ID. When using the Select Tag Helper with `asp-for="Course.DepartmentID"`, the selected item in the drop-down list is determined by the value of `Course.DepartmentID`, not by the `selectedDepartment` parameter passed to the SelectList constructor. Update `Pages/Courses/Edit.cshtml` with the following markup: