将方法传递给Razor Pages Textarea声明,以计算“ Readonly”在恐龙上
我正在尝试将方法传递到textarea
声明中(计算textarea
是否应为readonly
not 是否)
我得到了:
标签助手'textarea'不得在元素的属性声明区域中具有c#。
我在cshtml文件中有类似的内容:
<td>
<textarea class="form-control" asp-for="WipCommentCalculation.Comments!.PALaborComment" rows="1" @SetTextAreaVisibility(@Model.ManagerType, "PA" )>
</textarea>
</td>
.cs文件:
private string SetTextAreaVisibility(string ManagerType, string textboxType)
{
if (ManagerType == "_PAExpenditureLayout" && textboxType == "PA")
{
return "";
}
else
{
return "readonly";
}
}
这是一个缩短版本,因为我希望对该方法有更多的逻辑,但是这个想法是通过此方法:setTextAreaEvisibility(@model。托管类型,“ pa”)
进入Texarea声明,因此它可以计算出“ readonly”标签(是否可以使textarea
note)放置“ readonly”标签。 CSHTML页面有几个文本区域,根据状态,用户等,是否可以编辑。
如果您有另一个建议,我该怎么做,我也很高兴听到它。 谢谢。
I'm trying to pass a method into a textArea
declaration (to calculate if the textArea
should be readOnly
or not)
I'm getting:
The tag helper 'textarea' must not have C# in the element's attribute declaration area.
I have something like this in my cshtml file:
<td>
<textarea class="form-control" asp-for="WipCommentCalculation.Comments!.PALaborComment" rows="1" @SetTextAreaVisibility(@Model.ManagerType, "PA" )>
</textarea>
</td>
and the .cs file:
private string SetTextAreaVisibility(string ManagerType, string textboxType)
{
if (ManagerType == "_PAExpenditureLayout" && textboxType == "PA")
{
return "";
}
else
{
return "readonly";
}
}
This is a shortened version as I want a bit more logic into the method, but the idea would be to pass this method: SetTextAreaVisibility(@Model.ManagerType, "PA" )
into the texArea declaration so it can calculate wether it should put a "readonly" tag or not (to make editable the textArea
or not.
the cshtml page has several text areas, and depending a status, user, etc will be editable or not.
If you have another suggestion of how can I do this I'm also happy to hear about it.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在HTML中,有一个专门针对可读输入的属性。
因此,在您的情况下,您将将方法的返回类型更改为 bool ,然后将其绑定到textarea中 Readonly 属性的值。
这是一个工作示例:
html/cshtml:
c#方法:
用于ReadOnly属性的MDN文档: readonly文档
In HTML there is an attribute specifically made for readonly inputs.
So in your case, you will change the return type of the method to bool and then bind it to the value of readonly attribute in the textarea.
Here is a working example:
HTML/cshtml:
c# method:
MDN Docs for readonly attribute: readonly docs
您不能将方法传递给剃须刀textarea,但是您可以将setTextareaivisibily的结果传递给使用模型的剃须刀页面,以下是一个演示:
模型:
动作:view:
view:
You cannot pass methods to razor pages textArea,but you can pass the result of SetTextAreaVisibility to razor page with model,here is a demo:
Model:
action:
view:
认为您可以编写自定义标签助手。
要么将taghelpers注册到_viewimports.cshtml以供全局使用
,要么在页面顶部导入以使用
参考
asp.net core中的自定义标记助手
Thinking that you can write a custom tag helper.
Either Register the TagHelpers to _ViewImports.cshtml for global use
Or import at the top of the page to use
Reference
Condition Tag Helper
Custom Tag Helper in ASP.NET Core