asp.net 有条件地禁用标签助手(textarea) [英] asp.net conditionally disable a tag helper (textarea)
问题描述
我想根据从模型评估的条件启用或禁用 textarea,并且我正在使用 textarea 标签助手.换句话说,是这样的:
I want to enable or disable a textarea depending on a condition that evalueates from the model, and I am using the textarea tag helper. In other words, something like this:
<textarea asp-for="Doc" @(Model.MustDisable ? "disabled" : "")></textarea>
但我收到以下设计时错误:标签助手textarea"在元素的属性声明区域中不得包含 C#.
But I got the following design-time error: The tag helper 'textarea' must not have C# in element's attribute declaration area.
然后我尝试了:
<textarea asp-for="Doc" disabled='@(Model.MustDisable ? "disabled" : "")'></textarea>
没有显示任何设计时错误,但呈现如下:Model.MustDisable==true
呈现 disabled='disabled'
并且 Model.MustDisable==false
呈现 disabled
.因此文本区域将始终被禁用.
which did not show any design time error but it renders like this:
Model.MustDisable==true
renders disabled='disabled'
AND Model.MustDisable==false
renders disabled
.
So the text area will always be disabled.
然后我尝试(删除's):
Then I tried (removing the 's):
textarea asp-for="Doc" disabled=@(Model.MustDisable ? "disabled" : "")></textarea>
没有显示任何设计时错误,但呈现与前一个相同.
which did not show any design time error but it renders the same as the previous one.
我怎样才能以正确的方式实现这一点?
How can I implement this the right way?
推荐答案
其实很简单,disable 属性已经随心所欲了——你可以传入一个布尔值:
It is actually very simple, the disable attribute is already working as you want - you can pass in a boolean value:
<textarea asp-for="Doc" disabled="@Model.MustDisable"></textarea>
如果为 false,则不呈现 disabled
属性:
if false the disabled
attribute is not rendered:
<textarea></textarea>
如果为真,则 disabled
属性设置为disabled":
if true the disabled
attribute is set to "disabled":
<textarea disabled="disabled"></textarea>
这篇关于asp.net 有条件地禁用标签助手(textarea)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!