We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Value is not being selected based on another Dropdown List programmatically.

I have two dropdown list. One is province and the other one is city. Data are downloaded from an  API on OnInitializedAsync(). When province is selected, list of city is updated with a new list of cities with the help of

<DropDownListEvents TValue="string" ValueChange="OnChange" />

in province list. It works okay if manually selected with a mouse. But when programmatically selected because it's in edit mode, the province works fine but the city is not. The city list though is updated but the @bind-Value="@city" is not being selected thought the list is updated. Here's my code snippet:

<EjsDropDownList @ref="provinceObj" TValue="string" Placeholder="Province" DataSource="@provinces" @bind-Value="@provinceBindVal">
    <DropDownListFieldSettings Value="id" Text="provinceName" />
    <DropDownListEvents TValue="string" ValueChange="OnChange" />
 </EjsDropDownList>

<EjsDropDownList @ref="cityObj" TValue="string" Placeholder="Cities" DataSource="@cities" @bind-Value="@cityDefaultValue">
      <DropDownListFieldSettings Value="id" Text="cityName" />
 </EjsDropDownList>

public void OnPopupOpen(PopupOpenEventArgs<object> args) {
     cityDefaultValue2 = data.cityId.ToString();
     provinceBindVal = data.provinceId.ToString(); 
}

private void OnChange(ChangeEventArgs<string> args)
{
    for(int i = 0; i < provinces.Count(); i++)
            if (int.Parse(args.Value) == int.Parse(provinces[i].id))
                cities = provinces[i].provinceCities[0].cities;

        if (isEditMode)
        {
            for (int i = 0; i < cities.Count(); i++)
            {
                if (cityDefaultValue2 == cities[i].id)
                {
                    cityDefaultValue = cities[i].id;
                    break;
                }
            }
        }
}

I have already added StateHasChanged() and Task.Delay(400), still not getting the correct city.


3 Replies

SD Saranya Dhayalan Syncfusion Team November 12, 2019 01:53 PM UTC

Hi J, 
 
Thank you for contacting Syncfusion support 
 
We have checked your code snippets. We suspect the cause of the issue is the list item is not updated on initial time while set the value by programmatically. So, when we set Index property is -1, list items rendered properly. So, the value updated properly in the second DropdownList. Please find the below code snippet. 
 
<div class='control-wrapper'> 
    <EjsDropDownList @ref="provinceObj" TValue="string" Placeholder="Province" DataSource="@provinces" @bind-Value="@provinceBindVal" Index="-1"> 
        <DropDownListFieldSettings Text="ProvinceName" Value="ProvinceId" /> 
        <DropDownListEvents TValue="string" ValueChange="OnChange" /> 
    </EjsDropDownList> 
 
    <EjsDropDownList @ref="cityObj" TValue="string" Placeholder="Cities" DataSource="@cities" @bind-Value="@cityDefaultValue"> 
        <DropDownListFieldSettings Text="CityName" Value="CityId" /> 
    </EjsDropDownList> 
 
</div> 
 
<div> 
    <EjsButton CssClass="e-flat" Content="@Content" @onclick="OnClick"></EjsButton> 
</div> 
 
For your convenience, we have prepared a sample. Please find the sample link: 
 
 
Could you please check the above sample and get back to us, if you need any further assistance on this? 
 
Regards, 
Saranya D 



J j November 12, 2019 02:31 PM UTC

Thank you very much for the quick reply. More powers!


SD Saranya Dhayalan Syncfusion Team November 13, 2019 10:58 AM UTC

Hi J 
 
Most Welcome.  
 
Please let us know, if you need any further assistance on this.  
 
Regards, 
Saranya D 


Loader.
Live Chat Icon For mobile
Up arrow icon