I have many dropdown items. Shown a sample below. I am rewriting to make it easier to maintain.
Assume NavItem satisfies both PH1 and PH2. But btnReferences[i] returns “__internalId : null” at times for some of the items. For the items it works fine, it returns similar to below:
<button class="item" id="2-1" b-ly1z09b32bl_7>two</button>
May I know where I am going wrong.
Old Code:
@if (ItemTest.Contains("PH1"))
{
<li class="dropdown-item">
<button @ref="_btn1" class="item" id="0" @onclick=Open1>One</button>
</li>
<li class="dropdown-item">
<button @ref="_btn2" class="item" id="1" @onclick=Open2>Two</button>
</li>
}
@if (ItemTest.Contains("PH2"))
{
<li class="dropdown-item">
<button @ref="_btn3" class="item" id="2" @onclick=Open3>Three</button>
</li>
<li class="dropdown-item">
<button @ref="_btn4" class="item" id="3" @onclick=Open4> Four</button>
</li>
}
New code:
@for (var item = 1; item< 5; item++)
{
var i = item;
bool isAvailable = false;
if (!isAvailable && NavItem.Contains("PH1"))
isAvailable = i >= 0 && i <= 1;
if (!isAvailable && NavItem.Contains("PH2"))
isAvailable = i >= 2 && i <= 3;
if (isAvailable)
{
<li class="dropdown-item">
<button @ref="btnReferences[i]" class="item" id="@i-1" @onclick="(async ()=> await OpenDialog(i))">@name[i+1]</button>
</li>
}
}
List<string> name = new List<string>
{"nothing", "one", "two", "three", "four"};
private List<ElementReference> btnReferences = new List<ElementReference>();
protected override void OnInitialized()
{
for (int i = 0; i < 4; i++)
{
btnReferences.Add(new ElementReference());
}
}
//btnReferences[i] below returns "__internalId : null"
public async Task OpenDialog(int i)
{
await JSRuntime.InvokeVoidAsync("Print", btnReferences[i]);
}
btnReferences[i] in the above method returns “__internalId : null”. I expect it to refer appropriate button. Thank you.