Remove Selected Item causes binding value = null

Hi,

When added and removing the last selected item, it throw System.NullReferenceException: 'Object reference not set to an instance of an object.' on Binding value variable (string[] type)

You can reproduce by just copy code from this link.

https://blazor.syncfusion.com/documentation/multiselect-dropdown/data-binding/

It will be thrown at @foreach (var SelectedValue in >>>MultiVal)<<<

package version 18.4.0.43 (Single Package <PackageReference Include="Syncfusion.Blazor" Version="18.4.0.43" />)

Please check,
Passakorn




3 Replies 1 reply marked as answer

SP Sureshkumar P Syncfusion Team February 18, 2021 10:40 AM UTC

Hi Passakorn, 
 
Greetings from Syncfusion support. 
 
Thanks for your suggestion, when the MultiVal property has null value the foreach returns exceptions. So, we will change the below condition to resolve the issue. 
 
Please find the code changes here: 
@if (MultiVal != null) 
{ 
    @foreach (var SelectedValue in MultiVal) 
    { 
        <p>MultiSelect value is:<strong>@SelectedValue</strong></p> 
    } 
} 
 
<SfMultiSelect Placeholder="e.g. Australia" @bind-Value="@MultiVal" DataSource="@Country"> 
    <MultiSelectFieldSettings Value="Name"></MultiSelectFieldSettings> 
</SfMultiSelect> 
 
@code { 
 
    public string[] MultiVal { get; set; } = new string[] { }; 
 
    public class Countries 
    { 
        public string Name { getset; } 
 
        public string Code { getset; } 
    } 
 
    List<Countries> Country = new List<Countries> 
{ 
        new Countries() { Name = "Australia", Code = "AU" }, 
        new Countries() { Name = "Bermuda", Code = "BM" }, 
        new Countries() { Name = "Canada", Code = "CA" }, 
        new Countries() { Name = "Cameroon", Code = "CM" }, 
    }; 
} 
 
 
Also, we will resolve the sample level issue from our documentation. Once our document will publish, we will intimate you. We appreciate your patience. 
 
Regards, 
Sureshkumar P 
 


Marked as answer

MA MaTaO February 18, 2021 04:58 PM UTC

Hi Sureshkumar P ,

Thank for an update on document's code quote.

There is another suggestion that when we custom value type in MultiSelectFieldSettings.Value different from bind-Value array, there is no syntax error and compile error.
This will lead to when you remove an item from selected value, all selected values are removed from UI but not bind-Value array. Should it be some warning or syntax error that validate both variables to prevent this kind of problem?
I faced this problem by setting MultiSelectFieldSettings.Value to variable with int data type but bind-Value is string[].
By the way, I already solved this problem, just give some advice.

Thank you,
Passakorn


VS Vignesh Srinivasan Syncfusion Team February 21, 2021 05:44 PM UTC

Hi Passakorn, 
 
We have checked your reported query, when the MultiSelectFiledtSettings.value and bind-value are different data type so, preselected values does not removed from the DOM. It is because the entered different data type value is consider as custom value. When you click the clear button the value in the dataSource will be removed but the value has the different data type does not remove and does not throw any console errors. So, we suggest you to use the same data type for both bind-value and MultiSelectFiledtSettings.value to get rid of the reported issue. 
Please get back to us if you need any further assistance. 
 
Regards, 
 
Vignesh Srinivasan. 


Loader.
Up arrow icon