我正在嘗試將方法傳遞給textArea宣告(以計算是否textArea應該readOnly)
我越來越:
標簽助手“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";
}
}
這是一個縮短的版本,因為我想在方法中加入更多邏輯,但想法是將這個方法:傳遞SetTextAreaVisibility(@Model.ManagerType, "PA" )到 texArea 宣告中,以便它可以計算是否應該放置“只讀”標簽(以使可編輯textArea與否。cshtml 頁面有幾個文本區域,取決于狀態、用戶等是否可編輯。
如果您對我該怎么做有其他建議,我也很高興聽到。謝謝你。
uj5u.com熱心網友回復:
在 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;
}
}
只讀屬性的 MDN 檔案:只讀檔案
uj5u.com熱心網友回復:
您不能將方法傳遞給剃須刀頁面 textArea,但可以將 SetTextAreaVisibility 的結果傳遞給帶有模型的剃須刀頁面,這是一個演示:
模型:
public class TestModel {
public string ManagerType { get; set; }
public string status { get; set; }
}
行動:
public IActionResult Test()
{
var model = new TestModel { ManagerType= "_PAExpenditureLayout" };
model.status = SetTextAreaVisibility(model.ManagerType, "PA");
return View(model);
}
看法:
<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>
}
uj5u.com熱心網友回復:
認為您可以撰寫自定義標簽助手。
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 中的自定義標記幫助程式
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490590.html
