I have written a TagHelper called CanEditTagHelper that disables/enables input controls depending on a value passed in from the view.
The TagHelper looks like this:
[HtmlTargetElement("input", Attributes = CanEditAttribute)]
public class CanEditTagHelper : TagHelper
{
private const string CanEditAttribute = "asp-can-edit";
[HtmlAttributeName(CanEditAttribute)]
public bool CanEdit { set; get; }
public CanEditTagHelper(IHtmlGenerator generator)
{
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (!CanEdit)
{
output.Attributes["disabled"] = "disabled";
}
base.Process(context, output);
}
}
And it is used in the view like so...
<input asp-for="UserName" asp-can-edit='Model.CanEditMember("UserName")' />
This works well for regular inputs but doesn't seem to work for CheckBoxes (specifically, bootstrap checkboxes). The disabled attribute is never added to the checkbox input.
<div class="checkbox">
<label asp-for="Active" asp-context="label">
<input asp-for="Active"
asp-can-edit='Model.CanEditMember("Active")'
type="checkbox" />
Active
</label>
</div>
Does anyone know why this tag helper doesn't add a disabled attribute to checkboxes?
Aucun commentaire:
Enregistrer un commentaire