Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor conditional if statement for onclick

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?" : "")
like image 695
levis84 Avatar asked Dec 02 '22 08:12

levis84


1 Answers

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>
like image 174
Umair Avatar answered Dec 09 '22 15:12

Umair