0

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 class="dropdown-item">
            <input type="checkbox" id="cb4" data-id=3>
            <label for="cb4"> Item Height/label>
        </li>

         <li class="dropdown-item">
         <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.

1 Answer 1

2

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);       
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.