This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #5916
- Loading branch information
Showing
14 changed files
with
447 additions
and
103 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/Microsoft.AspNetCore.Mvc.TagHelpers/PartialTagHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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 System; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.TagHelpers | ||
{ | ||
/// <summary> | ||
/// Renders a partial view. | ||
/// </summary> | ||
[HtmlTargetElement("partial", Attributes = "name", TagStructure = TagStructure.WithoutEndTag)] | ||
public class PartialTagHelper : TagHelper | ||
{ | ||
private readonly IHtmlGenerator _htmlGenerator; | ||
private readonly IViewBufferScope _viewBufferScope; | ||
|
||
public PartialTagHelper( | ||
IHtmlGenerator htmlGenerator, | ||
IViewBufferScope viewBufferScope) | ||
{ | ||
_htmlGenerator = htmlGenerator ?? throw new ArgumentNullException(nameof(htmlGenerator)); | ||
_viewBufferScope = viewBufferScope ?? throw new ArgumentNullException(nameof(viewBufferScope)); | ||
} | ||
|
||
/// <summary> | ||
/// The name of the partial view used to create the HTML markup. Must not be <c>null</c>. | ||
/// </summary> | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// A model to pass into the partial view. | ||
/// </summary> | ||
public object Model { get; set; } | ||
|
||
/// <summary> | ||
/// A <see cref="ViewDataDictionary"/> to pass into the partial view. | ||
/// </summary> | ||
public ViewDataDictionary ViewData { get; set; } | ||
|
||
[ViewContext] | ||
public ViewContext ViewContext { get; set; } | ||
|
||
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) | ||
{ | ||
var viewBuffer = new ViewBuffer(_viewBufferScope, Name, ViewBuffer.PartialViewPageSize); | ||
using (var writer = new ViewBufferTextWriter(viewBuffer, Encoding.UTF8)) | ||
{ | ||
await _htmlGenerator.RenderPartialViewAsync( | ||
ViewContext, | ||
Name, | ||
Model, | ||
ViewData, | ||
writer); | ||
|
||
output.SuppressOutput(); | ||
output.Content.SetHtmlContent(viewBuffer); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.