Error updating the collection by program

Hello everyone,

i am using syncfusion 18.3.0.44 with Blazor WebAssembly (client side, not server side) and .net core 3.1 and i am getting an annoying error with the component SfListBox.

I can provide the following code as example:

"

<SfButton Content="AddElement" OnClick="@AddElementToList" />

<SfListBox @ref="ListBoxChat" TValue="int[]" TItem="Wrapper"
           DataSource="@Listed" CssClass="list">

    <ListBoxSelectionSettings Mode="SelectionMode.Single"></ListBoxSelectionSettings>
    <ListBoxEvents TValue="int[]" TItem="Wrapper"></ListBoxEvents>

    <ListBoxFieldSettings Text="Name" Value="ID" />
    <ListBoxTemplates TItem="Wrapper">
        <ItemTemplate>
            <i class="fa fa-@((context.IsSingle) ? "user" : "users") fa-2x"></i>
            <span class="top-left">@(context.Name)</span>
        </ItemTemplate>
    </ListBoxTemplates>
</SfListBox>

@code {

    public class Wrapper
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public bool IsSingle { get; set; }
        public int SortValue => ID;
    }

    public class MultiObservableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
    {
        public MultiObservableCollection() : base() { }
        public MultiObservableCollection(IEnumerable<T> collection) : base(collection) { }
        public MultiObservableCollection(List<T> list) : base(list) { }

        public void Reset(IEnumerable<T> range)
        {
            Items.Clear();
            AddRange(range);
        }

        public void AddRange(IEnumerable<T> range)
        {
            foreach (var item in range)
            {
                Items.Add(item);
            }
            FireChanges();
        }

        private void FireChanges()
        {
            OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Count"));
            OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Item[]"));
            OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
        }
    }


    SfListBox<int[], Wrapper> ListBoxChat;
    private bool UpdateListed = true;

    private List<Wrapper> Variable = new List<Wrapper>();
    private MultiObservableCollection<Wrapper> iListed = new MultiObservableCollection<Wrapper>();
    public MultiObservableCollection<Wrapper> Listed
    {
        get
        {
            if (UpdateListed)
            {
                // Working from syncfusion 18.3.0.44 on...
                var tmp = new List<Wrapper>();

                var fixedList = new List<Wrapper>
                {
                    new Wrapper { ID = -100, Name = "Alpha", IsSingle = true },
                    new Wrapper { ID = -101, Name = "Beta", IsSingle = true },
                    new Wrapper { ID = -102, Name = "Gamma", IsSingle = true }
                };

                tmp.AddRange(fixedList);

                if (Variable != null)
                    tmp.AddRange(Variable);

                iListed.Reset(tmp.OrderByDescending(o => o.SortValue));

                UpdateListed = false;
            }

            return iListed;
        }
    }

    void AddElementToList()
    {
        Variable.Add(new Wrapper { ID = iListed.Count + 1, Name = "Custom", IsSingle = false });
        UpdateListed = true;
    }
}

"

Every time that i click on the button "AddElement", i got the following error:

"
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Collection was modified; enumeration operation may not execute.
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
  at System.Collections.Generic.Dictionary`2+KeyCollection+Enumerator[TKey,TValue].MoveNext () <0x55823e8 + 0x00016> in <filename unknown>:0
  at Syncfusion.Blazor.DropDowns.SfListBox`2[TValue,TItem].OnParametersSetAsync () <0x44c3dc8 + 0x005fa> in <filename unknown>:0
  at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion (System.Threading.Tasks.Task task) <0x5582b80 + 0x000da> in <filename unknown>:0
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x43caea8 + 0x000b6> in <filename unknown>:0
"

If instead of an SfListBox i use a simple foreach loop

"
<ul>
    @foreach (var x in Listed)
    {
        <li>
            @x.Name
        </li>
    }
</ul>

"

that error does not occurs.


This is just a code example that mimics the behavior I observed in my actual code, where the collection of objects is updated from server input (and not by an interaction from the user).


Am I doing something wrong?


6 Replies 1 reply marked as answer

MK Mohan Kumar Ramasamy Syncfusion Team November 5, 2020 02:48 PM UTC

Hi M, 
 
We have checked your reported issue and we would like to let you know that it is a known issue. We have already logged an issue report for this, and We will include this fix in our upcoming patch release which will be scheduled on 11th November 2020. Until then we appreciate your patience. You can track the status in the below feedback link.  
 
 
Regards, 
Mohan kumar R 



M. M. November 6, 2020 08:41 AM UTC

Perfect, thanks for your kind response.


MK Mohan Kumar Ramasamy Syncfusion Team November 9, 2020 08:47 AM UTC

Hi M,  
  
Thanks for update. We will update further details as promised.  
  
Regards,  
Mohankumar R 



MK Mohan Kumar Ramasamy Syncfusion Team November 11, 2020 08:30 AM UTC

Hi M, 
 
Thank you for the patience. 
 
We are glad to announce that our weekly patch release (18.3.0.48) is rolled out. We have included the fix for the reported issue in this release. So, we suggest you upgrade our Syncfusion packages to our latest version to resolve this issue in your end. (18.3.0.48) 
 
Please let us know if you need further assistance on this. 
 
Regards, 
Mohan kumar R 


Marked as answer

M. M. November 11, 2020 09:53 AM UTC

I can confirm that the problem seems to be resolved, thanks for your work.


MK Mohan Kumar Ramasamy Syncfusion Team November 11, 2020 09:56 AM UTC

Hi M, 
 
Thanks for the update.  
 
We are happy to hear that your issue has been resolved. Please feel free to contact us if you need any further assistance on this. 
 
Regards, 
Mohan kumar R 


Loader.
Up arrow icon