TL; DR Are "partial Pages" possible in ASP.NET Core 2.0? If so, why am I getting the error described below?
I've been messing with the new Razor Page feature of ASP.NET 2.0 and having some trouble with partials. The documentation linked above refers to "partials" in several places, but only to the extent of saying mystical things like "The layouts, templates, and partials you're using with MVC controllers and conventional Razor views just work." Do they mean partial Views, like what we're used to, or some new kind of partial Page also? They show that files with the conventional Partial suffix in the name can be placed in the Pages/ folder, but the files that they mention (e.g., _ValidationScriptsPartial.cshtml) don't have the @page declaration. If "partial Pages" are indeed a new thing, then I would expect the following minimal scenario to work:
A Page file named
Derp.cshtml:@page @namespace MyApp.Pages @model DerpModel <p>Member 1: @Model.Member1</p> <p>Member 2: @Model.Member2</p>A class file named
Derp.cshtml.csin the same directory (nested under the first in VS 2017's Solution Explorer), containing the following class:namespace MyApp.Pages { public class DerpModel : PageModel { public void OnGet() { Member1 = "Derp Member1"; Member2 = "Derp Member2"; } public string Member1 { get; set; } public string Member2 { get; set; } } }A second Page file named
DerpWrapper.cshtmlin the same directory, with the following code:@page "{id}" @namespace MyApp.Pages @model DerpModel <p>Outer Member 1: @Model.Member1</p> <p>Outer Member 2: @Model.Member2</p> @Html.Partial("Derp", Model)And of course a
_ViewImportsfile that declares@namespace MyApp.Pages
Note that both Razor Pages have the same @model type, and the second Page basically wraps up the first one with a call to Html.Partial() and passes its Model. This should all be pretty standard stuff, but upon navigating to http://localhost:xxx/DerpWrapper, I get a 500 response due to a NullReferenceException with the following stack trace.
MyApp.Pages.Derp_Page.get_Model()
MyApp.Pages.Derp_Page+<ExecuteAsync>d__0.MoveNext() in Derp.cshtml
+
<p>Inner Member1: @Model.Member1</p>
...
MyApp.Pages.DerpWrapper_Page+<ExecuteAsync>d__0.MoveNext() in DerpWrapper.cshtml
+
@Html.Partial("Derp", Model)
...
Why does the "wrapper" Page's Model equal null? Is there no way to define a Razor Page and display it from another Page or View with Html.Partial()?
@pagedirective cause a page to behave as an action? That would imply having to use a child action to embed it instead of .Partial, but who knows. Anyhow, I'd just try removing the@pagefrom the partial Derp.cshtml. - AaronLS