Joe Salazar
Joe Salazar

Reputation: 400

From within the tag helper, how can I access the attributes that are not properties of the tag helper?

If I have the following tag helper:

using Microsoft.AspNetCore.Razor.TagHelpers;  

namespace CustomTagHelper.TagHelpers  
{  
    [HtmlTargetElement("my-first-tag-helper")]  
    public class MyCustomTagHelper : TagHelper  
    {  
        public string Id { get; set; }
        public string Type { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)  
        {  

            output.SuppressOutput();
            output.Content.Clear();

            output.Content.SetHtmlContent($"<input id={Id} type={Type} />");  
        }  
    }  
}  

and in the view I'm passing this:

<my-first-tag-helper id="my-input" type="text" placeholder="type anything you want" autocomplete="off" disabled="disabled" />

I want to be able to access any other attribute that has been added to that tag that is not a property of the tag helper so I can add them to the output. In this example those would be placeholder, autocomplete, and disabled.

In the previous example the resulting tag should be:

<input id="my-input" type="text" placeholder="type anything you want" autocomplete="off" disabled="disabled" />

So, how do I access those attributes that are not properties?

Upvotes: 1

Views: 1394

Answers (1)

Joe Salazar
Joe Salazar

Reputation: 400

I just stumbled across the answer to my own question.

The parameter named context that is passed to the method named Process contains the property AllAttributes which as the name implies contains all attributes that were passed regardless of whether they exist as a property or not. So then you just filter out the existing properties and then create the the attributes that are left in the list.

So this is what the solution looks like:

using Microsoft.AspNetCore.Razor.TagHelpers;  
using System.Collections.Generic;
using System.Linq;

namespace CustomTagHelper.TagHelpers  
{  
    [HtmlTargetElement("my-first-tag-helper")]  
    public class MyCustomTagHelper : TagHelper  
    {  
        public string Id { get; set; }
        public string Type { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)  
        {  
            output.SuppressOutput();
            output.Content.Clear();

            // ------------------- Begin Extra Attributes --------------------

            var attributeObjects = context.AllAttributes.ToList();

            var props = this.GetType().GetProperties().Select(p => p.Name);

            attributeObjects.RemoveAll(a => props.Contains(a.Name));

            var extraAttributeList = new List<string>();

            foreach (var attr in attributeObjects)
            {
                extraAttributeList.Add($"{attr.Name}=\"{attr.Value}\"");
            }                                                                                                 

            // ------------------- End Extra Attributes --------------------

            output.Content.SetHtmlContent($"<input id={Id} type={Type} {string.Join(" ", extraAttributeList)}/>");
        }  
    }  
}  

Upvotes: 3

Related Questions