locked
TagHelper not working RRS feed

  • Question

  • User1314407993 posted

    At one point this was working, and now I'm going nuts trying to figure out why it stopped working. I wrote a TagHelper for creating a checkbox list:

    [HtmlTargetElement("checkboxlist", Attributes = "asp-items, asp-model-name")]
        public class CheckboxListTagHelper : TagHelper
        {
            [HtmlAttributeName("asp-items")]
            public IEnumerable<SelectListItem> Items { get; set; }
    
            [HtmlAttributeName("asp-model-name")]
            public string ModelName { get; set; }
    
            [HtmlAttributeName("asp-list-class")]
            public string ListClass { get; set; }
    
            [HtmlAttributeName("asp-item-class")]
            public string ItemClass { get; set; }
    
            public override void Process(TagHelperContext context, TagHelperOutput output)
            {
                var sb = new StringBuilder();
    
                sb.Append($@"<ul class=""{ListClass}"">");
    
                var index = 0;
                foreach (var item in Items)
                {
                    var selected = item.Selected ? @"checked=""checked""" : "";
                    var disabled = item.Disabled ? @"disabled=""disabled""" : "";
    
                    sb.Append($@"<li class=""{ItemClass}"">");
                    sb.Append($@"<input type=""checkbox"" {selected} {disabled} id=""{ModelName}_{index}__Selected"" name=""{ModelName}[{index}].Selected"" value=""true"" /> ");
                    sb.Append($@"<label for=""{ModelName}_{index}__Selected"">{item.Text}</label>");
                    sb.Append($@"<input type=""hidden"" id=""{ModelName}_{index}__Text"" name=""{ModelName}[{index}].Text"" value=""{item.Text}"">");
                    sb.Append($@"<input type=""hidden"" id=""{ModelName}_{index}__Value"" name=""{ModelName}[{index}].Value"" value=""{item.Value}"">");
                    sb.Append($@"</li>");
    
                    index++;
                }
    
                sb.Append($@"</ul>");
    
                output.Content.AppendHtml(sb.ToString());
            }
        }

    I just pass in a SelectListItem collection, like so:

    <checkboxlist asp-items="@Model.SelectedJobPreferences" asp-model-name="SelectedJobPreferences"></checkboxlist>

    And wired up the TagHelper like this - I have a Razor Page that the TagHelper renders in, and regular Views as well. I used the same code to register in each's _ViewImports file:

    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    @addTagHelper *, ApplicationSourcing

    I've looked up likely reasons it's not working online, and everyone posts the code I've already used to register the helper. The tag just renders as shown in this output from Chrome:

    <checkboxlist asp-items="System.Collections.Generic.List`1[Microsoft.AspNetCore.Mvc.Rendering.SelectListItem]" asp-model-name="SelectedJobPreferences"></checkboxlist>

    Any ideas? 

    Thursday, June 20, 2019 6:33 PM

Answers

  • User1314407993 posted

    Found the answer!!! There were loads of suggestions, from setting compatibility versions to making sure the correct  _ViewImports file had the helper reference added to it. Then one post said the call to register the custom helpers had to point to the project/assembly name, not a namespace. We renamed the project recently, and although most of the namespaces were unaffected, it caused my Tag Helper to fail. I changed the "@tagHelper" line to use the project name, and it suddenly started working again. Hurray for idiocy. It's always something small and stupid...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 20, 2019 9:27 PM