title: blazor StateHasChanged not working in a loop
body: <p>I have a Blazor Server app.  I dynamically add a custom control (which I created in a Razor Class Library) to my page, like this:</p>
  <pre><code>&lt;div class=&quot;container &quot;&gt;
      &lt;div class=&quot;row &quot;&gt;
         &lt;div class=&quot;col-12&quot;&gt;
            &lt;label&gt;@p_section.question&lt;/label&gt;
         &lt;/div&gt;
      &lt;/div&gt;
     @foreach (object new_control_model in p_section.list_of_control_models)
      {         
           DisaggregateControlModel new_disag_model = (DisaggregateControlModel)new_control_model;                                                
           &lt;DisaggregateControl @ref=&quot;myComponents[new_disag_model.id]&quot; model=&quot;new_disag_model&quot;&gt;
           &lt;/DisaggregateControl&gt;                                                
      }
  &lt;/div&gt;
  </code></pre>
  <p>This add the control to my dictionary, which I can access.</p>
  <pre><code>@code {
     private Dictionary&lt;string, object&gt; myComponents = new Dictionary&lt;string, object&gt;();  
     
  }
  </code></pre>
  <p>In the custom control, I have a method that sets a bool, which allows me to display or hide a Div.  In the web component that has this code, I want to iterate over all the objects in myComponents and either turn on or off the div display.  I do  that like this:</p>
  <pre><code>foreach (string id in some_list_of_ids){
     //find the object in myComponents list
     object found_obj = myComponents.FirstOrDefault(x =&gt; x.Key == id).Value;
     //cast to my custom control
     DisaggregateControl myControl = (DisaggregateControl)found_obj;
     // based on property, determine if I should show the div or not
     if(myControl.some_vale &gt;0){
        //show
        myControl.showDiv(true);
     }else{
       myControl.showDiv(false);
    }
  }
  //updated all the controls, so update the page
  StateHasChanged();
  </code></pre>
  <p>If I debug and walk through, I can see that the code works.  The correct divs are shown/hidden.  Until the code reaches StateHasChanged(), and then all the divs are hidden.  If I remove StateHasChanged then the code also does not work (the divs are not shown, when they should be).</p>
  <p>I am not sure what the issue is or how to best handle this?</p>