Skip to content

Commit eb79fc3

Browse files
authored
Mention the Partial method for producing a PartialViewResult (dotnet#12790)
* Mention the Partial method for producing a PartialViewResult * Minor tweaks * Update sample project to 2.2 * Revert wording change * Simplify code snippet * Revert code snippet change * Fix code snippet dev lang
1 parent 08c7821 commit eb79fc3

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

aspnetcore/mvc/views/partial.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: ardalis
44
description: Discover how to use partial views to break up large markup files and reduce the duplication of common markup across web pages in ASP.NET Core apps.
55
ms.author: riande
66
ms.custom: mvc
7-
ms.date: 04/06/2019
7+
ms.date: 06/12/2019
88
uid: mvc/views/partial
99
---
1010
# Partial views in ASP.NET Core
@@ -42,7 +42,7 @@ Don't use a partial view where complex rendering logic or code execution is requ
4242

4343
A partial view is a *.cshtml* markup file maintained within the *Views* folder (MVC) or *Pages* folder (Razor Pages).
4444

45-
In ASP.NET Core MVC, a controller's <xref:Microsoft.AspNetCore.Mvc.ViewResult> is capable of returning either a view or a partial view. In Razor Pages, a <xref:Microsoft.AspNetCore.Mvc.RazorPages.PageModel> can return a <xref:Microsoft.AspNetCore.Mvc.PartialViewResult>. Referencing and rendering partial views is described in the [Reference a partial view](#reference-a-partial-view) section.
45+
In ASP.NET Core MVC, a controller's <xref:Microsoft.AspNetCore.Mvc.ViewResult> is capable of returning either a view or a partial view. In Razor Pages, a <xref:Microsoft.AspNetCore.Mvc.RazorPages.PageModel> can return a partial view represented as a <xref:Microsoft.AspNetCore.Mvc.PartialViewResult> object. Referencing and rendering partial views is described in the [Reference a partial view](#reference-a-partial-view) section.
4646

4747
Unlike MVC view or page rendering, a partial view doesn't run *_ViewStart.cshtml*. For more information on *_ViewStart.cshtml*, see <xref:mvc/views/layout>.
4848

@@ -54,7 +54,7 @@ Partial view file names often begin with an underscore (`_`). This naming conven
5454

5555
A partial view is a *.cshtml* markup file maintained within the *Views* folder.
5656

57-
A controller's <xref:Microsoft.AspNetCore.Mvc.ViewResult> is capable of returning either a view or a partial view.
57+
A controller's <xref:Microsoft.AspNetCore.Mvc.ViewResult> is capable of returning either a view or a partial view. Referencing and rendering partial views is described in the [Reference a partial view](#reference-a-partial-view) section.
5858

5959
Unlike MVC view rendering, a partial view doesn't run *_ViewStart.cshtml*. For more information on *_ViewStart.cshtml*, see <xref:mvc/views/layout>.
6060

@@ -64,6 +64,33 @@ Partial view file names often begin with an underscore (`_`). This naming conven
6464

6565
## Reference a partial view
6666

67+
::: moniker range=">= aspnetcore-2.0"
68+
69+
### Use a partial view in a Razor Pages PageModel
70+
71+
In ASP.NET Core 2.0 or 2.1, the following handler method renders the *\_AuthorPartialRP.cshtml* partial view to the response:
72+
73+
```csharp
74+
public IActionResult OnGetPartial() =>
75+
new PartialViewResult
76+
{
77+
ViewName = "_AuthorPartialRP",
78+
ViewData = ViewData,
79+
};
80+
```
81+
82+
::: moniker-end
83+
84+
::: moniker range=">= aspnetcore-2.2"
85+
86+
In ASP.NET Core 2.2 or later, a handler method can alternatively call the <xref:Microsoft.AspNetCore.Mvc.RazorPages.PageBase.Partial*> method to produce a `PartialViewResult` object:
87+
88+
[!code-csharp[](partial/sample/PartialViewsSample/Pages/DiscoveryRP.cshtml.cs?name=snippet_OnGetPartial)]
89+
90+
::: moniker-end
91+
92+
### Use a partial view in a markup file
93+
6794
::: moniker range=">= aspnetcore-2.1"
6895

6996
Within a markup file, there are several ways to reference a partial view. We recommend that apps use one of the following asynchronous rendering approaches:

aspnetcore/mvc/views/partial/sample/PartialViewsSample/Pages/DiscoveryRP.cshtml.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@ public class DiscoveryModel : PageModel
77
{
88
public void OnGet() => Page();
99

10-
public IActionResult OnGetPartial()
11-
{
12-
return new PartialViewResult
13-
{
14-
ViewName = "_AuthorPartialRP",
15-
ViewData = ViewData,
16-
};
17-
}
18-
10+
#region snippet_OnGetPartial
11+
public IActionResult OnGetPartial() =>
12+
Partial("_AuthorPartialRP");
13+
#endregion
1914
}
2015
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.1</TargetFramework>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
88
<PackageReference Include="Microsoft.AspNetCore.App" />
9-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
109
</ItemGroup>
1110

1211
</Project>

aspnetcore/mvc/views/partial/sample/PartialViewsSample/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public void ConfigureServices(IServiceCollection services)
2626
options.MinimumSameSitePolicy = SameSiteMode.None;
2727
});
2828

29-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
29+
services.AddMvc()
30+
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
3031
}
3132

3233
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

0 commit comments

Comments
 (0)