-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
make Partial Tag Helper's "view-data" attribute helperful #9736
Comments
I find myself often needing to pass view data into the partial views and I find it quite cumbersome. I find the below code example to be rather cumbersome, and also it is not obvious what relation the ViewData and the partial have. @{
ViewData["flavor"] = "raspberry"
}
<partial name="Pie" /> I too would much prefer: <partial name="Pie" view-data-flavor="raspberry" /> |
Also, doing it this way: @{
ViewData["flavor"] = "raspberry"
}
<partial name="Pie" /> pollutes the parent ViewData as opposed to only affecting the partial view. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
should this be very easy ? and after process, clear the private |
We can get around this for the time being by implementing a custom TagHelper. using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Example.TagHelpers;
[HtmlTargetElement("partial", Attributes = viewDataAttributeNamePrefix + "*")]
public class ViewDataTagHelper : TagHelper
{
private const string viewDataAttributeNamePrefix = "ex-view-data-";
/// <inheritdoc />
public override int Order => -1000;
[HtmlAttributeNotBound, ViewContext]
public required ViewContext ViewContext { get; set; }
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var attributes = context.AllAttributes.Where(attr =>
attr.Name.StartsWith(viewDataAttributeNamePrefix)
);
foreach (var attribute in attributes)
{
var key = attribute.Name.Substring(viewDataAttributeNamePrefix.Length);
var value = attribute.Value;
ViewContext.ViewData[key] = value;
}
return Task.CompletedTask;
}
}; Notice the attribute name cannot conflict with Usage: <partial name ="_MyPartial" ex-view-data-id="123" ex-view-data-returnUrl="/dashboard" /> |
The
IDictionary<string,object>
has a very helpful way to add item , But this line of code throw exception because view-data is null .I suggestion to either support this razor syntax or add a new attribute to add addiontal item to the partial view and not effect the current view‘s ViewData
The text was updated successfully, but these errors were encountered: