Skip to content

view-data-* attribute for partial tag helper #60459

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion src/Mvc/Mvc.TagHelpers/src/PartialTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class PartialTagHelper : TagHelper

private readonly ICompositeViewEngine _viewEngine;
private readonly IViewBufferScope _viewBufferScope;
private ViewDataDictionary _viewData;

/// <summary>
/// Creates a new <see cref="PartialTagHelper"/>.
Expand Down Expand Up @@ -91,7 +92,19 @@ public object Model
/// <summary>
/// A <see cref="ViewDataDictionary"/> to pass into the partial view.
/// </summary>
public ViewDataDictionary ViewData { get; set; }
[HtmlAttributeName("view-data", DictionaryAttributePrefix = "view-data-")]
public ViewDataDictionary ViewData
{
get
{
if (_viewData == null)
{
_viewData = new ViewDataDictionary(ViewContext.ViewData);
}
return _viewData;
}
set => _viewData = value;
}

/// <summary>
/// Gets the <see cref="Rendering.ViewContext"/> of the executing view.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@page

@{
ViewData["Title"] = "PartialWithViewData";
}

<div>
<partial name="_PrintViewData" view-data-id="123" view-data-returnUrl='"/dashboard"' />
</div>

<div>
<partial name="_PrintViewData" view-data-id="456" view-data-name='"John Doe"' />
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h4>ViewData Values</h4>
<dl>
@foreach (var key in ViewData.Keys)
{
<dt>@key</dt>
<dd>@ViewData[key]</dd>
}
</dl>
Loading