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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:

Created:

Updated:

Platform:

Replies:

149009 Nov 12,2019 03:57 AM UTC Nov 13,2019 10:58 AM UTC Blazor 3
loading
Tags: Dropdown List
j
Asked On November 12, 2019 03:57 AM UTC

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.


Saranya Dhayalan [Syncfusion]
Replied On 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
Replied On November 12, 2019 02:31 PM UTC

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

Saranya Dhayalan [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon