How to write a custom ASP.NET 5 taghelper that contains other taghelpers
Asked Answered
P

1

5

I've been looking at examples on taghelpers on google but couldn't find any example I'm looking for.

I have the following code:

<div class="form-group">
    <label asp-for="PersonName" class="col-md-2 control-label"></label>
    <div class="col-md-10">
        <input asp-for="PersonName" class="form-control" />
        <span asp-validation-for="PersonName" class="text-danger"></span>
    </div>
</div>

What i'd look to do is replace it with something like

<bootstraprow asp-for="PersonName"></bootstraprow>

However I'm not sure to write a taghelper that contains other taghelpers

  1. Is it possible?
  2. If possible provide code example for the above

Edit: It is not the same as storing variables in custom taghelpers but I want to call other custom taghelpers or existing taghelpers.

Paschall answered 11/11, 2015 at 11:2 Comment(3)
Possible duplicate of Nesting TagHelpers in ASP.NET 5 MVC 6Tiffa
Not a duplicate. The "possible duplicate" only mention about nesting TagHelpers. This one is about TagHelpers generating markup that will be handled by another TagHelper. Which is not possible at the moment.Stationmaster
You are asking if you can write tag helpers that contain other tag helpers and if so, provide an example. In my opinion the related question covers both.Tiffa
S
2

If we check what you have, the only property that you are using is PersonName. As for the markup itself, everything else is good old HTML.

So you don't need to replace anything. What you need is to have constructor that has a dependency on IHtmlGenerator. This will get automatically injected and you will be able to generate the different tags based on your model.

Relevant IHtmlGenerator Signature:

public interface IHtmlGenerator
{
    ...

    TagBuilder GenerateValidationMessage(
        ViewContext viewContext,
        string expression,
        string message,
        string tag,
        object htmlAttributes);
    TagBuilder GenerateLabel(
        ViewContext viewContext,
        ModelExplorer modelExplorer,
        string expression,
        string labelText,
        object htmlAttributes);
    TagBuilder GenerateTextBox(
        ViewContext viewContext,
        ModelExplorer modelExplorer,
        string expression,
        object value,
        string format,
        object htmlAttributes);
    ...
}

And that's it!

Here's a bit of code that would capture the basic tag:

[HtmlTargetElement("bootstraprow")]
public BootstrapRowTagHelper: TagHelper
{
    protected IHtmlGenerator Generator { get; set; }
    public InputTagHelper(IHtmlGenerator generator)
    {
        Generator = generator;
    }

    [HtmlAttributeName("asp-for")]
    public ModelExpression For { get; set; }

    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        //todo: write your html generating code here.
    }
}

Here's a repo with sample code that generates Bootstrap HTML from TagHelpers:

https://github.com/dpaquette/TagHelperSamples/blob/master/TagHelperSamples/src/TagHelperSamples.Bootstrap/

Stationmaster answered 11/11, 2015 at 13:34 Comment(1)
Can you provide the code to reference the above in cshtml?Paschall

© 2022 - 2024 — McMap. All rights reserved.