I have a span which should have an onclick attribute if the IsActive bool is true. Otherwise the span should have no onclick attribute.
e.g.
@if (IsActive == true)
{
<span @onclick="@(e => Go.DoSomething("please"))">
@s.DisplayText
</span>
}
else
{
<span>
@s.DisplayText
</span>
}
Is there not a way to avoid the repeated code using a ternary operator? e.g.
@(IsActive == true ? "add onclick method somehow?" : "")
A better way to add the condition IsActive == true
is in the Go.DoSomething
method. But ideally I would have used a button if its clickable because we can add a disabled
attribute to a button, in your case you can add the condition inside the onclick
method.
Just a tip for the button, you can just add your c# boolean property within that attribute like this:
<button disabled="@IsActive">Save</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With