-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Custom TagHelper for checkbox does not work #5968
Comments
Could you please share with us your TagHelper code so we can take a look? |
It is my simplified code. [HtmlTargetElement("input", Attributes = ForAttributeName)]
public class CheckedTagHelper : TagHelper {
private const string ForAttributeName = "asp-for";
[HtmlAttributeName(ForAttributeName)]
public ModelExpression For { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output) {
if (For.Metadata.ModelType == typeof(bool)) {
var tb = new TagBuilder("input");
tb.AddCssClass("myclass");
output.MergeAttributes(tb);
}
}
} |
@gushwell thanks for posting the code. This attribute specifies that you want the tag helper to apply to all [HtmlTargetElement("input", Attributes = ForAttributeName)] You'd need to specify additional restrictions on that attribute to have it apply only when the Is that what you're trying to do? If so, change it to something like this: [HtmlTargetElement("input", Attributes = ForAttributeName + ",[type=checkbox]")] |
@Eilon Thank you for your reply. I tried changing the attribute as follows, but it did not work as intended.
Also, changing it as follows (this was what I wanted to do), but it did not work as intended.
In this case, the Process method is certainly called. I am using Microsoft.ASPNetCore.Mvc.TagHelpers (1.1.2). |
@NTaylorMullen did I mess up the tag helper selector syntax? Any thoughts on this? |
@gushwell maybe try removing all the extra spaces in your attribute? So instead of:
Try:
? |
@Eilon I changed the attribute and tried it. pattern-1input
output
pattern-2input
output
Question. |
@NTaylorMullen can you take a look? |
@gushwell so the reason why your pattern-2 works and the first one does not is for multiple reasons.
Hopefully these reasoning's shed some light on what you're seeing! You can work around the issue mentioned above by manually merging your class attribute instead of relying on the |
@NTaylorMullen can you file a new bug about clearing up how the checkbox tag helper works per our discussion? |
Do we have working CheckboxTagHelper and final link of working code (with example would be great!) |
I wrote a custom TagHelper class.
In this Process method, a new value is added to the class-attribute of checkbox, but no value is added.
In line number 197 of InputTagHelper.cs, the return value of GenerateCheckBox () has not been assigned, is it related to this problem?
The text was updated successfully, but these errors were encountered: