I created a tag helper that inherits from InputTagHelper as shown by an answer in this post https://mcmap.net/q/1146081/-input-tag-helper-not-working-with-razor-code.
Here is the code
[HtmlTargetElement("input", Attributes = ForAttributeName)]
public class ExrInputTagHelper : InputTagHelper
{
private const string ForAttributeName = "asp-for";
[HtmlAttributeName("asp-disabled")]
public bool IsDisabled { get; set; }
public ExrInputTagHelper(IHtmlGenerator generator):base(generator)
{
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (IsDisabled)
{
var d = new TagHelperAttribute("disabled", "disabled");
output.Attributes.Add(d);
}
base.Process(context, output);
}
}
This is its usage:
<input asp-for="UsingCreditCard" type="checkbox" asp-disabled="@Model.UsingACH" />
This works great but has one glaring issue. If the input type is a checkbox, it is rendered twice. All the other input types work great. Why would this be happening?
<input checked="checked" data-val="true" data-val-required="The UsingCreditCard field is required." id="UsingCreditCard" name="UsingCreditCard" type="checkbox" value="true">
<input checked="checked" id="UsingCreditCard" name="UsingCreditCard" type="checkbox" value="true">
Any thoughts are greatly appreciated.
Thanks in advance.