To display a disabled input element, you just need to add the disabled attribute. All below displays a disabled text input element.
<input type="checkbox" disabled /> <input type="checkbox" disabled="disabled" /> <input type="checkbox" disabled="false" /> <input type="checkbox" disabled="no" /> <input type="checkbox" disabled="enabled" /> <input type="checkbox" disabled="why is it still disabled" />
In Asp.NET Core, you can extend an existing input tag helper to create an auxiliary read-only input tag.
Extend the InputTagHelper class, add a new property to determine whether the input should be disabled or not, and based on this value add the attribute "disabled" to the input.
[HtmlTargetElement("input", Attributes = ForAttributeName)] public class MyCustomTextArea : InputTagHelper { private const string ForAttributeName = "asp-for"; [HtmlAttributeName("asp-is-disabled")] public bool IsDisabled { set; get; } public MyCustomTextArea(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); } }
Now, to use this special textarea helper, you need to call the addTagHelper method in _ViewImports.cshtml .
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @addTagHelper *, YourAssemblyNameHere
Now, in your opinion, you can specify the value of the asp-is-disabled attribute.
<input type="text" asp-for="OtherDrugs" asp-is-disabled="@Model.OtherDrugs==null"/>
Shyju source share