将方法传递给Razor Pages Textarea声明,以计算“ Readonly”在恐龙上

发布于 2025-02-07 01:36:16 字数 1049 浏览 1 评论 0原文

我正在尝试将方法传递到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安稳善良 2025-02-14 01:36:16

在HTML中,有一个专门针对可读输入的属性。

因此,在您的情况下,您将将方法的返回类型更改为 bool ,然后将其绑定到textarea中 Readonly 属性的值。

这是一个工作示例:

html/cshtml:

<td>
    <textarea class="form-control"
              asp-for="WipCommentCalculation.Comments!.PALaborComment"
              rows="1"
              readonly="@SetTextAreaVisibility(@Model.ManagerType, "PA")"> 
    </textarea>
</td>

c#方法:

private bool SetTextAreaVisibility(string ManagerType, string textboxType)
{
    if (ManagerType == "_PAExpenditureLayout" && textboxType == "PA")
    {
        return false;
    }
    else
    {
        return true;
    }
}

用于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:

<td>
    <textarea class="form-control"
              asp-for="WipCommentCalculation.Comments!.PALaborComment"
              rows="1"
              readonly="@SetTextAreaVisibility(@Model.ManagerType, "PA")"> 
    </textarea>
</td>

c# method:

private bool SetTextAreaVisibility(string ManagerType, string textboxType)
{
    if (ManagerType == "_PAExpenditureLayout" && textboxType == "PA")
    {
        return false;
    }
    else
    {
        return true;
    }
}

MDN Docs for readonly attribute: readonly docs

孤君无依 2025-02-14 01:36:16

您不能将方法传递给剃须刀textarea,但是您可以将setTextareaivisibily的结果传递给使用模型的剃须刀页面,以下是一个演示:

模型:

public class TestModel {
        public string ManagerType { get; set; }
        public string status { get; set; }

    }

动作:view:

public IActionResult Test()
        {
            var model = new TestModel {  ManagerType= "_PAExpenditureLayout" };

            model.status = SetTextAreaVisibility(model.ManagerType, "PA");
            return View(model);
        }

view:

<td><textarea class="form-control" asp-for="WipCommentCalculation.Comments!.PALaborComment" rows="1" @SetTextAreaVisibility(@Model.ManagerType, "PA" )></textarea></td>
    @section Scripts{ 
        <script>
        $(function () {
            if ("@Model.status"== "readonly") {
                $('textarea').each(function () {
                    $(this).attr("readonly", true);
                });
            }
            
        })
        </script>
    
    }

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:

public class TestModel {
        public string ManagerType { get; set; }
        public string status { get; set; }

    }

action:

public IActionResult Test()
        {
            var model = new TestModel {  ManagerType= "_PAExpenditureLayout" };

            model.status = SetTextAreaVisibility(model.ManagerType, "PA");
            return View(model);
        }

view:

<td><textarea class="form-control" asp-for="WipCommentCalculation.Comments!.PALaborComment" rows="1" @SetTextAreaVisibility(@Model.ManagerType, "PA" )></textarea></td>
    @section Scripts{ 
        <script>
        $(function () {
            if ("@Model.status"== "readonly") {
                $('textarea').each(function () {
                    $(this).attr("readonly", true);
                });
            }
            
        })
        </script>
    
    }
旧人哭 2025-02-14 01:36:16

认为您可以编写自定义标签助手。

using Microsoft.AspNetCore.Razor.TagHelpers;

namespace CustomTagHelpers.TagHelpers
{
    [HtmlTargetElement(Attributes = "is-readonly")]
    public class ReadonlyTagHelper : TagHelper
    {
        public bool IsReadonly { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (IsReadonly)
            {
                output.Attributes.SetAttribute("readonly", "readonly");
            }
        }
    }
}

要么将taghelpers注册到_viewimports.cshtml以供全局使用

@addTagHelper CustomTagHelpers.*, TagHelpers

,要么在页面顶部导入以使用

@using CustomTagHelpers.TagHelpers
<textarea class="form-control"
          asp-for="WipCommentCalculation.Comments!.PALaborComment"
          rows="1"
          is-readonly="@SetTextAreaVisibility(@Model.ManagerType, "PA")"> 
</textarea>

参考

asp.net core中的自定义标记助手

Thinking that you can write a custom tag helper.

using Microsoft.AspNetCore.Razor.TagHelpers;

namespace CustomTagHelpers.TagHelpers
{
    [HtmlTargetElement(Attributes = "is-readonly")]
    public class ReadonlyTagHelper : TagHelper
    {
        public bool IsReadonly { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (IsReadonly)
            {
                output.Attributes.SetAttribute("readonly", "readonly");
            }
        }
    }
}

Either Register the TagHelpers to _ViewImports.cshtml for global use

@addTagHelper CustomTagHelpers.*, TagHelpers

Or import at the top of the page to use

@using CustomTagHelpers.TagHelpers
<textarea class="form-control"
          asp-for="WipCommentCalculation.Comments!.PALaborComment"
          rows="1"
          is-readonly="@SetTextAreaVisibility(@Model.ManagerType, "PA")"> 
</textarea>

Reference

Condition Tag Helper

Custom Tag Helper in ASP.NET Core

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文