ASP.NETCoreInputTagHelpers不使用RazorCode不能让这两种技术一起工作。我只是想根据视图模型属性的值在输入字段上设置禁用属性。当我将razor代码放在asp-for标签之后时,razorintellisense无法识别并且字段未按预期禁用...呈现输出...当我将razor代码放在asp-for标签之前时,标签helperintellisense失败识别,并且该字段未按预期设置视图模型属性...呈现输出...请注意,如果razor代码位于类属性中,则组合标签助手和razor有效。不幸的是,输入字段需要一个disabled属性,而不是bootstrap3的disabled类。有没有办法让它工作?要呈现禁用的输入元素,只需添加disabled属性。以下所有内容都将呈现禁用的输入文本元素。在Asp.NETCore中,您可以扩展现有的输入标签助手来创建只读输入标签助手。扩展InputTagHelper类,添加一个新属性来标识是否应禁用输入,并根据此值向输入添加“已禁用”属性。[HtmlTargetElement("input",Attributes=ForAttributeName)]publicclassMyCustomTextArea:InputTagHelper{privateconststringForAttributeName="asp-for";[HtmlAttributeName("asp-is-disabled")]publicboolIsDisabled{set;得到;}publicMyCustomTextArea(IHtmlGeneratorgenerator):base(generator){}publicoverridevoidProcess(TagHelperContextcontext,TagHelperOutputoutput){if(IsDisabled){vard=newTagHelperAttribute("disabled","disabled");输出.Attributes.Add(d);}base.Process(上下文,输出);现在要使用这个自定义文本区域助手,您需要调用_ViewImports.cshtml中的addTagHelper方法。@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers@addTagHelper*,YourAssemblyNameHere现在您可以在您的视图中指定asp-is-disabled属性值。您可以像这样使用ASPCore标记助手:然后将[editable(false)]添加到您的属性中,如下所示:[Editable(false)]publicstringName{set;get;}然后您应该扩展InputTagHelper:[HtmlTargetElement("input",Attributes=ForAttributeName)]publicclassExtendedInputTagHelper:InputTagHelper{privateconststringForAttributeName="asp-for";publicExtendedInputTagHelper(IHtmlGeneratorgenerator):base(generator){}publicoverridevoidProcess(TagHelperContextcontext,TagHelperOutputoutput){varisContentModified=output.IsContentModified;如果(For.Metadata.IsReadOnly){varattribute=newTagHelperAttribute("disabled","disabled");output.Attributes.Add(属性);}if(!isContentModified){base.Process(context,output);最后在_ViewImports.cshtml中导入TagHelper:@addTagHelper*,这种方案的好处是把逻辑放在模型中,保留了MVC原则。你最好做这样的事情:@{varisDisabled=((Model.OtherDrugs==null)?"disabled":string.Empty());}那么以上就是C#学习教程:ASP.NETCoreInputTagHelperNotUseRazor代码分享的所有内容,如果对你有用还需要进一步了解C#学习教程,希望大家多多付出注意—@Html.TextBox("OtherDrugs","",new{isDisabled})本文来自网络合集,不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
