Skip to content

Make Partial on PageBase and PageModel work correctly #13013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ public abstract partial class PageBase : Microsoft.AspNetCore.Mvc.Razor.RazorPag
{
protected PageBase() { }
public Microsoft.AspNetCore.Http.HttpContext HttpContext { get { throw null; } }
public Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider MetadataProvider { get { throw null; } set { } }
public Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary ModelState { get { throw null; } }
public Microsoft.AspNetCore.Mvc.RazorPages.PageContext PageContext { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public Microsoft.AspNetCore.Http.HttpRequest Request { get { throw null; } }
Expand Down Expand Up @@ -500,6 +501,7 @@ public abstract partial class PageModel : Microsoft.AspNetCore.Mvc.Filters.IAsyn
{
protected PageModel() { }
public Microsoft.AspNetCore.Http.HttpContext HttpContext { get { throw null; } }
public Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider MetadataProvider { get { throw null; } set { } }
public Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary ModelState { get { throw null; } }
[Microsoft.AspNetCore.Mvc.RazorPages.PageContextAttribute]
public Microsoft.AspNetCore.Mvc.RazorPages.PageContext PageContext { get { throw null; } set { } }
Expand Down
104 changes: 55 additions & 49 deletions src/Mvc/Mvc.RazorPages/src/PageBase.cs

Large diffs are not rendered by default.

108 changes: 57 additions & 51 deletions src/Mvc/Mvc.RazorPages/src/PageModel.cs

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions src/Mvc/Mvc.RazorPages/test/PageModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,13 +1898,15 @@ public async Task AsyncPageHandlerSelectingMethod_InvokeSyncMethods()
public void PartialView_WithName()
{
// Arrange
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
var modelMetadataProvider = new EmptyModelMetadataProvider();
var viewData = new ViewDataDictionary(modelMetadataProvider, new ModelStateDictionary());
var pageModel = new TestPageModel
{
PageContext = new PageContext
{
ViewData = viewData
}
},
MetadataProvider = modelMetadataProvider,
};

// Act
Expand All @@ -1913,20 +1915,22 @@ public void PartialView_WithName()
// Assert
Assert.NotNull(result);
Assert.Equal("LoginStatus", result.ViewName);
Assert.Same(viewData, result.ViewData);
Assert.Null(result.Model);
}

[Fact]
public void PartialView_WithNameAndModel()
{
// Arrange
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
var modelMetadataProvider = new EmptyModelMetadataProvider();
var viewData = new ViewDataDictionary(modelMetadataProvider, new ModelStateDictionary());
var pageModel = new TestPageModel
{
PageContext = new PageContext
{
ViewData = viewData
}
},
MetadataProvider = modelMetadataProvider,
};
var model = new { Username = "Admin" };

Expand All @@ -1937,7 +1941,6 @@ public void PartialView_WithNameAndModel()
Assert.NotNull(result);
Assert.Equal("LoginStatus", result.ViewName);
Assert.Equal(model, result.Model);
Assert.Same(viewData, result.ViewData);
}

[Fact]
Expand Down
17 changes: 11 additions & 6 deletions src/Mvc/Mvc.RazorPages/test/PageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1700,36 +1700,42 @@ public void StatusCode_SetsStatusCode()
public void PartialView_WithName()
{
// Arrange
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
var modelMetadataProvider = new EmptyModelMetadataProvider();
var viewData = new ViewDataDictionary(modelMetadataProvider, new ModelStateDictionary());
var pageModel = new TestPage
{
ViewContext = new ViewContext
{
ViewData = viewData
}
},
MetadataProvider = modelMetadataProvider,
};
viewData.Model = pageModel;

// Act
var result = pageModel.Partial("LoginStatus");

// Assert
Assert.NotNull(result);
Assert.Equal("LoginStatus", result.ViewName);
Assert.Same(viewData, result.ViewData);
Assert.Null(result.Model);
}

[Fact]
public void PartialView_WithNameAndModel()
{
// Arrange
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
var modelMetadataProvider = new EmptyModelMetadataProvider();
var viewData = new ViewDataDictionary(modelMetadataProvider, new ModelStateDictionary());
var pageModel = new TestPage
{
ViewContext = new ViewContext
{
ViewData = viewData
}
},
MetadataProvider = modelMetadataProvider,
};
viewData.Model = pageModel;
var model = new { Username = "Admin" };

// Act
Expand All @@ -1739,7 +1745,6 @@ public void PartialView_WithNameAndModel()
Assert.NotNull(result);
Assert.Equal("LoginStatus", result.ViewName);
Assert.Equal(model, result.Model);
Assert.Same(viewData, result.ViewData);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/Mvc/samples/MvcSandbox/Pages/PagesHome.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@page
@model TestModel
@model PagesHome
@{

ViewData["Title"] = "Hello from pages";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MvcSandbox
{
public class TestModel : PageModel
public class PagesHome : PageModel
{
public string Name { get; private set; } = "World";

Expand Down
32 changes: 26 additions & 6 deletions src/Mvc/test/Mvc.FunctionalTests/RazorPagesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,43 @@ public async Task Page_Handler_ReturnTypeImplementsIActionResult()
}

[Fact]
public async Task Page_Handler_ReturnPartialWithoutModel()
public async Task PageWithoutModel_ReturnPartial()
{
// Act
var document = await Client.GetHtmlDocumentAsync("RenderPartialWithoutModel");
using var document = await Client.GetHtmlDocumentAsync("PageWithoutModelRenderPartial");

var element = document.RequiredQuerySelector("#content");
Assert.Equal("Welcome, Guest", element.TextContent);
Assert.Equal("Hello from Razor Page", element.TextContent);
}

[Fact]
public async Task Page_Handler_ReturnPartialWithModel()
public async Task PageWithModel_Works()
{
// Act
var document = await Client.GetHtmlDocumentAsync("RenderPartialWithModel");
using var document = await Client.GetHtmlDocumentAsync("RenderPartial");

var element = document.RequiredQuerySelector("#content");
Assert.Equal("Welcome, Admin", element.TextContent);
Assert.Equal("Hello from RenderPartialModel", element.TextContent);
}

[Fact]
public async Task PageWithModel_PartialUsingPageModelWorks()
{
// Act
using var document = await Client.GetHtmlDocumentAsync("RenderPartial/UsePageModelAsPartialModel");

var element = document.RequiredQuerySelector("#content");
Assert.Equal("Hello from RenderPartialWithModel", element.TextContent);
}

[Fact]
public async Task PageWithModel_PartialWithNoModel()
{
// Act
using var document = await Client.GetHtmlDocumentAsync("RenderPartial/NoPartialModel");

var element = document.RequiredQuerySelector("#content");
Assert.Equal("Hello default", element.TextContent);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace RazorPagesWebSite.Models
{
public class RenderPartialModel
{
public string Value { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@page
@using RazorPagesWebSite.Models

@functions {
public IActionResult OnGet() => Partial("_RenderPartial", new RenderPartialModel { Value = "Hello from Razor Page" });
}
7 changes: 7 additions & 0 deletions src/Mvc/test/WebSites/RazorPagesWebSite/RenderPartial.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@page "{handler?}"
@model RazorPagesWebSite.RenderPartialWithModel

@{
throw new Exception("This should not be called");

}
20 changes: 20 additions & 0 deletions src/Mvc/test/WebSites/RazorPagesWebSite/RenderPartial.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesWebSite.Models;

namespace RazorPagesWebSite
{
public class RenderPartialWithModel : PageModel
{
public string Text { get; set; } = $"Hello from {nameof(RenderPartialWithModel)}";

public IActionResult OnGet() => Partial("_RenderPartial", new RenderPartialModel { Value = $"Hello from {nameof(RenderPartialModel)}" });

public IActionResult OnGetUsePageModelAsPartialModel() => Partial("_RenderPartialPageModel", this);

public IActionResult OnGetNoPartialModel() => Partial("_RenderPartial");
}
}
15 changes: 0 additions & 15 deletions src/Mvc/test/WebSites/RazorPagesWebSite/RenderPartialWithModel.cs

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@using RazorPagesWebSite.Models
@model RenderPartialModel

<span id="content">@(Model?.Value ?? "Hello default")</span>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@model RazorPagesWebSite.RenderPartialWithModel

<span id="content">Welcome, @Model.Username</span>
<span id="content">@Model.Text</span>