Consider the following code :
In MyPage.razor :
<ul>
@foreach (var item in myList)
{
var theBool = false;
<li>
<span>For @item, theBool is @theBool</span>
(<a href="" @onclick="@(e => theBool = !theBool)">Reverse bool for @item</a>)
</li>
}
</ul>
@code {
List<string> myList = new() { "item1", "item2", "item3", "item4", "item5", };
}
... which produce the following output :
I expect that clicking on a link will switch the preceding bool value, so it shows "True" instead of "False". It's not working at all, the variable seems to be changed but the UI (the <span>
) is not refreshed. What is the good way to implement this in Blazor Webassembly ?
(I hope not to be forced to use the @code
section : it would be a major code overhead in term of maintainability).
UPDATE 1
This is what I'm trying to achieve : A click on a link (which is outside of a component) has to change the parameter of the component (to change the component behavior) :
<ul>
@foreach (var item in myList)
{
var theBool = false;
<li>
<a href="" @onclick="@(e => theBool = !theBool)">Show/Hide details</a>
<MyCoolComponent DisplayDetails="@theBool"></MyCoolComponent>
</li>
}
</ul>