Search code examples
c#asp.net-coreasp.net-core-tag-helpers

Is it possible to use the same TagHelperContext in a partial view?


I have this view (which is working fine):

<form asp-action="Edit" asp-orientation="Vertical" cit-ajax-modal="true">
    <modal id="language-modal" title="Hello World!">
        <modal-body>
            <validation-summary/>
            <form-group-select asp-for="CultureName" asp-items="@GlobalizationViewService.GetCultureSelectList()"/>
            <form-group-text asp-for="DisplayName"/>
            <form-group-checkbox asp-for="IsEnabled"/>
            <form-group-numeric asp-for="DefaultChance" />
        </modal-body>
        <modal-footer/>
    </modal>
</form>

Now I want to extract the form-group-* taghelpers into a partial view:

<validation-summary/>
<form-group-select asp-for="CultureName" asp-items="@GlobalizationViewService.GetCultureSelectList()"/>
<form-group-text asp-for="DisplayName"/>
<form-group-checkbox asp-for="IsEnabled"/>
<form-group-numeric asp-for="DefaultChance" />

Those TagHelpers using a TagHelperContext created by my FormTagHelper element.

So my new view is this:

<form asp-action="Edit" asp-orientation="Vertical" cit-ajax-modal="true">
    <modal id="language-modal" title="Hello World!">
        <modal-body>
            @Html.Partial("EditElementsPartial")
        </modal-body>
        <modal-footer/>
    </modal>
</form>

However, the TagHelperContext in the Partial does not include any Items created from my FormTagHelper.

My Basic motivation is to create a common View, which is used by an Update or Create view. form-group-* should get information about the asp-orientation from the form tag.

Is Html.Partial the wrong aproach to the problem? Any better way? Is it possible to pass the TagHelperContext to the Partial?


Solution

  • The only way to achieve what you want is to use ViewContext.ViewData for tag helpers communication, as discussed here: https://github.com/aspnet/Mvc/issues/3233#issuecomment-146027741