Checking if "Item1" exists in ArrayOfItems.
For example, if I check simple condition like @if("Item1 == "Item1"), it returns true and displays dropdown items. But ArrayOfItems.Contains("Item1") always returns false, even though ArrayOfItems has "Item1" and "Item2"
@if (ArrayOfItems.Contains("Item1")) // this always returns false
{
<li >
<input type="checkbox" id="cb4" data-id=3>
<label for="cb4"> Item Height/label>
</li>
<li >
<input type="checkbox" id="cb5" data-id=4>
<label for="cb5"> Item Width </label>
</li>
}
@code {
public string ItemName{ get; set; }
List<string> ArrayOfItems= new List<string>();
private void HandleNameSent(string ItemName)
{
JSRuntime.InvokeVoidAsync("console.log", "ItemName", ItemName);
ArrayOfItems.Add(ItemName);
JSRuntime.InvokeVoidAsync("console.log", "ArrayOfItems", ArrayOfItems); // returns an array with two values "Item1" and "Item2"
}
}
May I know what could be the reason. Thank you.
CodePudding user response:
Initially when the page was rendered, ArrayOfItems
was empty.
Whenever data gets updated after the page has already initialized, you need to call StateHasChanged()
to re-render with the updated data.
private void HandleNameSent(string ItemName)
{
JSRuntime.InvokeVoidAsync("console.log", "ItemName", ItemName);
ArrayOfItems.Add(ItemName);
StateHasChanged();
JSRuntime.InvokeVoidAsync("console.log", "ArrayOfItems", ArrayOfItems);
}
CodePudding user response:
Check this question here: If/else is not working properly with using OnInitiliazedAsync in Blazor C#
I hope you'll get the answer. You need to add a check first.