Reputation: 11905
I am writing a set of tag helpers that target, for example, <form>
and <input>
elements. I want to add a custom attribute to the <form>
element, and retrieve the value of that attribute in the contained <input>
element. So, if my HTML looks like this:
<form xx-value='123'>
<input asp-for='Something' />
</form>
then in my InputTagHelper
I would want to retrieve the value 123 that was specified for the xx-value
attribute.
Is there a designed-in way to pass data like this between tag helpers?
Consider the case where I have this markup:
<form xx-value='123'>
<input asp-for='Something' />
</form>
<form>
<input asp-for='SomethingElse' />
</form>
In this case, the first invocation of the InputTagHelper
would get the value 123. But the second invocation of the InputTagHelper
would get a value of 0 since its parent <form>
tag didn't specify the magic xxx-value attribute.
Upvotes: 1
Views: 925
Reputation: 11905
The simple answer (which doesn't work for <form>
and <input>
tags - see blow) is for the "parent" tag helper to store the value in the context.Items
dictionary and for the "child" tag helper(s) to retrieve the value from that same dictionary. A Google search for "child tag helper" yields many examples of this scheme.
The problem with this answer (in the context of the OP) is that, for some reason, the <form>
tag helper executes after its child <input>
tag helper. So, rather than receiving the value from the parent FormTagHelper
, the InputTagHelper
discovers that the context.Items
dictionary is empty.
I created this SO post to ask about that weird behavior.
Upvotes: 2