3

Is it possible to build complex taghelper in ASP.NET 5 where the custom tag have child elements/tags of a certain type?

<blockSection columns="2" labelPosition="left">
   <inputField for="name" />
   <inputField for="email" required="true"/>
</blockSection>

The in the above example, the blockSection will be a TagHelper that only accepts inputField tags.

N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
sam360
  • 1,121
  • 3
  • 18
  • 37

1 Answers1

3

Without a whole bunch of trickery (parsing a tags body yourself or creating a TagHelper that targets everything) you can't do this 100% today (beta6).

You can partially fix your issue by ensuring <inputField> elements only appear inside of a <blockSection> tag (would not restrict you from putting things like <p> inside of a <blockSection>). Can be accomplished by using context.Items bag to notify an InputFieldTagHelper that it's (or is not) encapsulated by a <blockSection>. See this issue for information on how to communicate between child => parent.

If you're willing to wait for this Razor issue to be completed; you'll be able to enforce what tags can appear inside your TagHelper.

Another similar SO issue for reference.

Community
  • 1
  • 1
N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
  • Thanks for the response. This would be highly needed, any ideas when this be resolved and be available? – sam360 Aug 10 '15 at 02:15
  • It's next on my todo list. Hopefully beta7. Worst case beta8. :) – N. Taylor Mullen Aug 10 '15 at 02:59
  • How do we future proof these tags? let's say W3C decides to make "blockSection" an official supported HTML5.x or 6.x tag? I think the was a good way to namespace it! Not sure why the MVC team went down the C# way, now back to XML tags... – sam360 Aug 11 '15 at 00:10
  • You can always use the `@tagHelperPrefix "SomePrefix:"` to make it clear where TagHelpers are. By doing this it means you'd write your blockSection as so: `` – N. Taylor Mullen Aug 11 '15 at 02:38
  • Amazing!! this does exactly what I needed. – sam360 Aug 12 '15 at 20:35