title: ASP.NET Core- Is there a way to render group of elements each using custom tag-helpers?
body: <p>I noticed in my project that all my form fields follow the same pattern. A typical example is:</p>
  
  <pre><code>&lt;div class="col-x-x"&gt;
    &lt;label asp-for="Property"&gt;&lt;/label&gt;
    &lt;span message="description"&gt;
    &lt;input asp-for="Property" /&gt;
    &lt;span asp-validation-for="Property"&gt;&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I would love to have some way of grouping this code so that i simply pass it the property on the model and it outputs the correct HTML. e.g.:</p>
  
  <pre><code>&lt;form-field for="Property" ...&gt;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>@Html.StringFormField(...)
  </code></pre>
  
  <p>The issue I am having is that whatever method I try, the html outputted is the original html above, and not the html that is generated from the tag helpers. I have tried both methods and neither have been successful. Additionally I have tried to create a razor function, but all my attempts fail to compile, and I can't make a partial view work as I haven't been able to find a way to get the property information after passing a string to a view.</p>
  
  <p>My latest attempt was using a tag helper, however this had the same issue mentioned previously. The latest version of the code is as follows:</p>
  
  <pre><code>[HtmlTargetElement("form-field", Attributes = "for")]
  public class FormFieldTagHelper : TagHelper
  {
      [HtmlAttributeName("for")]
      public ModelExpression For { get; set; }
  
      public override void Process(TagHelperContext context, TagHelperOutput output)
      {
          output.TagName = "div";
          output.TagMode = TagMode.StartTagAndEndTag;
  
          var contentBuilder = new HtmlContentBuilder();
  
          contentBuilder.AppendHtmlLine($"&lt;label asp-for=\"{For}\"&gt;&lt;/label&gt;");
          contentBuilder.AppendHtmlLine($"&lt;span message=\"description.\"&gt;&lt;/span&gt;");
          contentBuilder.AppendHtmlLine($"&lt;input asp-for=\"{For}\"/&gt;");
          contentBuilder.AppendHtmlLine($"&lt;span asp-validation-for=\"{For}\"/&gt;&lt;/span&gt;");
  
          output.Content.SetHtmlContent(contentBuilder);
      }
  }
  </code></pre>
  
  <p>There is an issue addressing this (<a href="https://stackoverflow.com/questions/56754809/use-razor-tag-helpers-inside-custom-tag-helper">with no solution</a>) which suggested the order of the imports was a potential issue, so my imports are as follows:</p>
  
  <pre><code>@addTagHelper Project.Web.Features.Shared.*, Project.Web
  @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
  </code></pre>
  
  <p>Any solution would be welcome, either for a tag helper or another method.</p>