From 6f06ff799762b24124e5fc1d1b97938ec964a5e5 Mon Sep 17 00:00:00 2001 From: "Jeff Weiner (OFFICE)" Date: Wed, 12 Jun 2019 19:22:04 -0700 Subject: [PATCH 1/2] Move SelectList code-generation into OnPageHandlerExecutionAsync `SelectList` initializers are only being code-generated into the GET handlers, but not the POST handlers. Since `.ViewData` entries do not persist across responses, the page will not render correctly when the POST handler does not redirect (i.e. because validation failed). This change moves the initialization into a reusable `OnPageHandlerExecutionAsync`, which is invoked after model binding but before the handler itself. No need to dual-generate the initializers into both handlers! --- .../Bootstrap3/CreatePageModel.cshtml | 21 +++++++++++++----- .../Bootstrap3/EditPageModel.cshtml | 22 ++++++++++++++----- .../Bootstrap4/CreatePageModel.cshtml | 21 +++++++++++++----- .../Bootstrap4/EditPageModel.cshtml | 22 ++++++++++++++----- .../Resources/RazorPages/CarCreateCs.txt | 8 ++++++- .../RazorPages/CarCreateCsWithDAL.txt | 8 ++++++- .../Resources/RazorPages/Crud/CreateCs.txt | 8 ++++++- .../Resources/RazorPages/Crud/EditCs.txt | 9 +++++++- 8 files changed, 91 insertions(+), 28 deletions(-) diff --git a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/CreatePageModel.cshtml b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/CreatePageModel.cshtml index 13282bcf8..d07a76d65 100644 --- a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/CreatePageModel.cshtml +++ b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/CreatePageModel.cshtml @@ -40,12 +40,6 @@ namespace @Model.NamespaceName public IActionResult OnGet() { -@{ - foreach (var property in relatedProperties.Values) - { - @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName"); - } -} return Page(); } @@ -64,5 +58,20 @@ namespace @Model.NamespaceName return RedirectToPage("./Index"); } +@if (relatedProperties.Any()) +{ + + public override Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) + { +@{ + foreach (var property in relatedProperties.Values) + { + @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName"); + } +} + + return base.OnPageHandlerExecutionAsync(context, next); + } +} } } \ No newline at end of file diff --git a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/EditPageModel.cshtml b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/EditPageModel.cshtml index ffb91f3c3..571766a70 100644 --- a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/EditPageModel.cshtml +++ b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/EditPageModel.cshtml @@ -61,12 +61,7 @@ namespace @Model.NamespaceName { return NotFound(); } -@{ - foreach (var property in relatedProperties.Values) - { - @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName"); - } -} + return Page(); } @@ -97,6 +92,21 @@ namespace @Model.NamespaceName return RedirectToPage("./Index"); } +@if (relatedProperties.Any()) +{ + + public override Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) + { +@{ + foreach (var property in relatedProperties.Values) + { + @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName"); + } +} + + return base.OnPageHandlerExecutionAsync(context, next); + } +} private bool @(Model.ModelTypeName)Exists(@primaryKeyShortTypeName id) { diff --git a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/CreatePageModel.cshtml b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/CreatePageModel.cshtml index 13282bcf8..d07a76d65 100644 --- a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/CreatePageModel.cshtml +++ b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/CreatePageModel.cshtml @@ -40,12 +40,6 @@ namespace @Model.NamespaceName public IActionResult OnGet() { -@{ - foreach (var property in relatedProperties.Values) - { - @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName"); - } -} return Page(); } @@ -64,5 +58,20 @@ namespace @Model.NamespaceName return RedirectToPage("./Index"); } +@if (relatedProperties.Any()) +{ + + public override Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) + { +@{ + foreach (var property in relatedProperties.Values) + { + @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName"); + } +} + + return base.OnPageHandlerExecutionAsync(context, next); + } +} } } \ No newline at end of file diff --git a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/EditPageModel.cshtml b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/EditPageModel.cshtml index ffb91f3c3..571766a70 100644 --- a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/EditPageModel.cshtml +++ b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/EditPageModel.cshtml @@ -61,12 +61,7 @@ namespace @Model.NamespaceName { return NotFound(); } -@{ - foreach (var property in relatedProperties.Values) - { - @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName"); - } -} + return Page(); } @@ -97,6 +92,21 @@ namespace @Model.NamespaceName return RedirectToPage("./Index"); } +@if (relatedProperties.Any()) +{ + + public override Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) + { +@{ + foreach (var property in relatedProperties.Values) + { + @:ViewData["@(property.ForeignKeyPropertyNames[0])"] = new SelectList(_context.@property.EntitySetName, "@property.PrimaryKeyNames[0]", "@property.DisplayPropertyName"); + } +} + + return base.OnPageHandlerExecutionAsync(context, next); + } +} private bool @(Model.ModelTypeName)Exists(@primaryKeyShortTypeName id) { diff --git a/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCs.txt b/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCs.txt index 4c3607fdc..ce02d4bc9 100644 --- a/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCs.txt +++ b/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCs.txt @@ -21,7 +21,6 @@ namespace TestProject public IActionResult OnGet() { - ViewData["ManufacturerID"] = new SelectList(_context.Set(), "ID", "ID"); return Page(); } @@ -40,5 +39,12 @@ namespace TestProject return RedirectToPage("./Index"); } + + public override Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) + { + ViewData["ManufacturerID"] = new SelectList(_context.Set(), "ID", "ID"); + + return base.OnPageHandlerExecutionAsync(context, next); + } } } \ No newline at end of file diff --git a/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCsWithDAL.txt b/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCsWithDAL.txt index a0f6ecb7d..cd6d68c90 100644 --- a/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCsWithDAL.txt +++ b/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCsWithDAL.txt @@ -21,7 +21,6 @@ namespace TestProject public IActionResult OnGet() { - ViewData["ManufacturerID"] = new SelectList(_context.Set(), "ID", "ID"); return Page(); } @@ -40,5 +39,12 @@ namespace TestProject return RedirectToPage("./Index"); } + + public override Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) + { + ViewData["ManufacturerID"] = new SelectList(_context.Set(), "ID", "ID"); + + return base.OnPageHandlerExecutionAsync(context, next); + } } } \ No newline at end of file diff --git a/test/E2E_Test/Compiler/Resources/RazorPages/Crud/CreateCs.txt b/test/E2E_Test/Compiler/Resources/RazorPages/Crud/CreateCs.txt index 7be944b14..e5bddda9f 100644 --- a/test/E2E_Test/Compiler/Resources/RazorPages/Crud/CreateCs.txt +++ b/test/E2E_Test/Compiler/Resources/RazorPages/Crud/CreateCs.txt @@ -21,7 +21,6 @@ namespace TestProject public IActionResult OnGet() { - ViewData["ManufacturerID"] = new SelectList(_context.Set(), "ID", "ID"); return Page(); } @@ -40,5 +39,12 @@ namespace TestProject return RedirectToPage("./Index"); } + + public override Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) + { + ViewData["ManufacturerID"] = new SelectList(_context.Set(), "ID", "ID"); + + return base.OnPageHandlerExecutionAsync(context, next); + } } } \ No newline at end of file diff --git a/test/E2E_Test/Compiler/Resources/RazorPages/Crud/EditCs.txt b/test/E2E_Test/Compiler/Resources/RazorPages/Crud/EditCs.txt index c4a5a9503..4af2695f7 100644 --- a/test/E2E_Test/Compiler/Resources/RazorPages/Crud/EditCs.txt +++ b/test/E2E_Test/Compiler/Resources/RazorPages/Crud/EditCs.txt @@ -37,7 +37,7 @@ namespace TestProject { return NotFound(); } - ViewData["ManufacturerID"] = new SelectList(_context.Set(), "ID", "ID"); + return Page(); } @@ -69,6 +69,13 @@ namespace TestProject return RedirectToPage("./Index"); } + public override Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) + { + ViewData["ManufacturerID"] = new SelectList(_context.Set(), "ID", "ID"); + + return base.OnPageHandlerExecutionAsync(context, next); + } + private bool CarExists(string id) { return _context.Car.Any(e => e.ID == id); From 24109fa12f45d9748d61544a4d8a62cbdf464a2c Mon Sep 17 00:00:00 2001 From: "Jeff Weiner (OFFICE)" Date: Thu, 13 Jun 2019 12:32:09 -0700 Subject: [PATCH 2/2] Forgot to add using statements for Mvc.Filters --- .../RazorPageGenerator/Bootstrap3/CreatePageModel.cshtml | 4 ++++ .../RazorPageGenerator/Bootstrap3/EditPageModel.cshtml | 4 ++++ .../RazorPageGenerator/Bootstrap4/CreatePageModel.cshtml | 4 ++++ .../RazorPageGenerator/Bootstrap4/EditPageModel.cshtml | 4 ++++ test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCs.txt | 1 + .../Compiler/Resources/RazorPages/CarCreateCsWithDAL.txt | 1 + test/E2E_Test/Compiler/Resources/RazorPages/Crud/CreateCs.txt | 1 + test/E2E_Test/Compiler/Resources/RazorPages/Crud/EditCs.txt | 1 + 8 files changed, 20 insertions(+) diff --git a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/CreatePageModel.cshtml b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/CreatePageModel.cshtml index d07a76d65..6b0b980bf 100644 --- a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/CreatePageModel.cshtml +++ b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/CreatePageModel.cshtml @@ -6,6 +6,10 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +@if (relatedProperties.Any()) +{ +using Microsoft.AspNetCore.Mvc.Filters; +} using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; @{ diff --git a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/EditPageModel.cshtml b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/EditPageModel.cshtml index 571766a70..8b9e3fc00 100644 --- a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/EditPageModel.cshtml +++ b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap3/EditPageModel.cshtml @@ -6,6 +6,10 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +@if (relatedProperties.Any()) +{ +using Microsoft.AspNetCore.Mvc.Filters; +} using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; diff --git a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/CreatePageModel.cshtml b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/CreatePageModel.cshtml index d07a76d65..6b0b980bf 100644 --- a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/CreatePageModel.cshtml +++ b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/CreatePageModel.cshtml @@ -6,6 +6,10 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +@if (relatedProperties.Any()) +{ +using Microsoft.AspNetCore.Mvc.Filters; +} using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; @{ diff --git a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/EditPageModel.cshtml b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/EditPageModel.cshtml index 571766a70..8b9e3fc00 100644 --- a/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/EditPageModel.cshtml +++ b/src/VS.Web.CG.Mvc/Templates/RazorPageGenerator/Bootstrap4/EditPageModel.cshtml @@ -6,6 +6,10 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +@if (relatedProperties.Any()) +{ +using Microsoft.AspNetCore.Mvc.Filters; +} using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; diff --git a/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCs.txt b/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCs.txt index ce02d4bc9..4c6d4e983 100644 --- a/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCs.txt +++ b/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCs.txt @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Library1.Models; diff --git a/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCsWithDAL.txt b/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCsWithDAL.txt index cd6d68c90..d7905a3b2 100644 --- a/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCsWithDAL.txt +++ b/test/E2E_Test/Compiler/Resources/RazorPages/CarCreateCsWithDAL.txt @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using DAL; diff --git a/test/E2E_Test/Compiler/Resources/RazorPages/Crud/CreateCs.txt b/test/E2E_Test/Compiler/Resources/RazorPages/Crud/CreateCs.txt index e5bddda9f..66f3a6312 100644 --- a/test/E2E_Test/Compiler/Resources/RazorPages/Crud/CreateCs.txt +++ b/test/E2E_Test/Compiler/Resources/RazorPages/Crud/CreateCs.txt @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Library1.Models; diff --git a/test/E2E_Test/Compiler/Resources/RazorPages/Crud/EditCs.txt b/test/E2E_Test/Compiler/Resources/RazorPages/Crud/EditCs.txt index 4af2695f7..6b630a9c6 100644 --- a/test/E2E_Test/Compiler/Resources/RazorPages/Crud/EditCs.txt +++ b/test/E2E_Test/Compiler/Resources/RazorPages/Crud/EditCs.txt @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore;