asp.net mvc - ASP .NET Core Input Tag Helper Not Working with Razor Code -


i want combine input tag helper razor code set attribute cannot 2 technologies work together. trying set disabled attribute on input field based on value of view model property.

when put razor code after asp-for tag razor intellisense not recognized , field not disabled expected...

<input asp-for="otherdrugs" @((model.otherdrugs == null) ? "disabled" : "") class="form-control" /> 

rendered output...

<input type="text" id="otherdrugs" name="otherdrugs" value="" /> 

when put razor code before asp-for tag tag helper intellisense not recognized , field not set view model properties expected...

<input @((model.otherdrugs == null) ? "disabled" : "") asp-for="otherdrug" class="form-control" /> 

rendered output...

<input disabled asp-for="otherdrugs" class="form-control" /> 

note combining tag helpers , razor work if razor code inside class attribute. unfortunately input fields require disabled attribute , not disabled class bootstrap 3.

is there way make work?

to render disabled input element, need add disabled attribute. below render disabled input text 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 still disabled" /> 

in asp.net core, can extend existing input tag helper create readonly input tag helper.

extend inputtaghelper class, add new property identify whether input should disabled or not , based on value, add "disabled" attribute 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 use custom textarea helper, need call addtaghelper method in _viewimports.cshtml.

@addtaghelper *, microsoft.aspnetcore.mvc.taghelpers @addtaghelper *, yourassemblynamehere 

now in view, can specify asp-is-disabled attribute value.

<input type="text" asp-for="otherdrugs"                                    asp-is-disabled="@model.otherdrugs==null"/>  

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -