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 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.
CodePudding user response:
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
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
CodePudding user response:
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 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>
}
CodePudding user response:
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
asp-for="WipCommentCalculation.Comments!.PALaborComment"
rows="1"
is-readonly="@SetTextAreaVisibility(@Model.ManagerType, "PA")">
</textarea>
Reference