Skip to content
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

Open
John0King opened this issue Apr 25, 2019 · 5 comments · May be fixed by #60459
Open

make Partial Tag Helper's "view-data" attribute helperful #9736

John0King opened this issue Apr 25, 2019 · 5 comments · May be fixed by #60459
Labels
affected-few This issue impacts only small number of customers area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team severity-major This label is used by an internal tool
Milestone

Comments

@John0King
Copy link
Contributor

<partial name="view" view-data-additionalItem="abc" /> 

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

@pranavkm pranavkm added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Apr 25, 2019
@mkArtakMSFT mkArtakMSFT added 1 - Ready enhancement This issue represents an ask for new feature or an enhancement to an existing one help wanted Up for grabs. We would accept a PR to help resolve this issue labels May 13, 2019
@mkArtakMSFT mkArtakMSFT added this to the Backlog milestone May 13, 2019
@vanillajonathan
Copy link
Contributor

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" />

@pranavkm pranavkm added affected-few This issue impacts only small number of customers bug This issue describes a behavior which is not expected - a bug. severity-major This label is used by an internal tool and removed enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Nov 6, 2020
@Neme12
Copy link
Contributor

Neme12 commented Jun 16, 2021

Also, doing it this way:

@{
    ViewData["flavor"] = "raspberry"
}
<partial name="Pie" />

pollutes the parent ViewData as opposed to only affecting the partial view.

@TanayParikh TanayParikh modified the milestones: Backlog, .NET 7 Planning Oct 19, 2021
@ghost
Copy link

ghost commented Oct 22, 2021

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.

@John0King
Copy link
Contributor Author

should this be very easy ?
the partial tag helper can access parent ViewData and add an new IDictionary<string,object> inside partial tag helper ad attributes helper.
OnProcess first populate the parent ViewData to an private IDictionary<string,object> , and then add the attributes IDictionary<string,object>

and after process, clear the private IDictionary<string,object> and the attributes IDictionary<string,object>

@mkArtakMSFT mkArtakMSFT added help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team and removed help wanted Up for grabs. We would accept a PR to help resolve this issue labels Oct 28, 2023
@chekkan
Copy link

chekkan commented Feb 16, 2025

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 view-data, and i had to prefix it with ex-.

Usage:

<partial name ="_MyPartial" ex-view-data-id="123" ex-view-data-returnUrl="/dashboard" />

chekkan added a commit to chekkan/aspnetcore that referenced this issue Feb 18, 2025
@chekkan chekkan linked a pull request Feb 18, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affected-few This issue impacts only small number of customers area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team severity-major This label is used by an internal tool
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants