0
votes

I am running a AspNetCore application and trying to create a custom TagHelper, its just not rendering. I have read quite a few articles and i think i have everything i need.

project.json

"Microsoft.AspNetCore.Mvc.TagHelpers": "1.1.3",

_ViewImport.cshtml

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

ReverseTextDivTagHelper.cs

The below code i got from an example i was trying

[HtmlTargetElement("tag-name")]
    public class ReverseTextDivTagHelper : TagHelper
    {
        private string _divData = string.Empty;
        public string DivData
        {
            get
            {
                char[] reversedData = _divData.ToCharArray();
                Array.Reverse(reversedData);
                String sDataReversed = new String(reversedData);
                return AllCaps ? sDataReversed.ToUpper() : sDataReversed;
            }
            set { _divData = value; }
        }

        public bool AllCaps { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "div";
            output.Content.SetContent(DivData);
        }
    }

And then in a partial view i am calling it like this

<reverse-text-div all-caps="true" div-data="Jacques"></reverse-text-div>
1
Can you please share entire content of _ViewImports.cshtml? Have you added ASP.NET Core web app namespace @addTagHelper "*, AspNetCoreWebApp1"?Sanket
Yes i have added itR4nc1d
Add HtmlAttributeName for your properties e.g. [HtmlAttributeName("all-caps")]Sanket
Sure will try it and let you knowR4nc1d
its weird so i got it working by simplifying the name and removing the [HtmlTargetElement("tag-name")]. I am gona investigate some more and try and figure out what the actual cause itR4nc1d

1 Answers

1
votes

Point 1: [HtmlTargetElement("tag-name")] has to be same as actual HTML tag i.e. reverse-text-div. It should be [HtmlTargetElement("reverse-text-div")]

Point 2: Apply HtmlAttributeName for your properties e.g. [HtmlAttributeName("all-caps")]