Change selected value of DropDown from code behind

I have a form with a RadzenDropDown with an object list bound to it.

The idea is a user can search that drop down to select a value (normal usage, works fine)

OR they can add a new object via a button.

When they click the add button, I create the new object and set the property of the main object to the Id of this new object, all is well functionally.

What I'd like to do it refresh the RadzenDropDown with the latest list (as it was plus the new object just created) and then select that new value so the user sees what they have selected (they 'selected' it by creating a new one).

Basically 're loading' the dropdown I think would do it, don't see a reload method like exists for a datagrid...

Any direction appreciated.

In Blazor this can be done with two way binding - i.e @bind-Value=someVariable. Every change no matter in the variable or in the component will be synced.

Hello! I tried that but that's not working for me, i have the next code:

private async void OnAddMatchPersonClick()
{
MatchPerson matchPerson = await DialogService.OpenAsync("Add MatchPerson", null);
if(matchPerson is not null)
{
this.radzenDropDown.Reset();
this.matchFaceTemplate.MatchPersonId = matchPerson.MatchPersonId;
}
}

And

<RadzenDropDown @ref="this.radzenDropDown" Data="@matchPeopleForMatchPersonId" TextProperty="Name" ValueProperty="MatchPersonId" AllowClear=true
Placeholder="Choose MatchPerson" style="display: block; width: 100%" @bind-Value=@matchFaceTemplate.MatchPersonId Name="MatchPersonId"/>

Do not use async voids:

But that way I cant open the Dialog.

You should use async Task.

Sorry for the insistence but is not working. I need to configure something on the RadzenDropDown component?

1 Like

@P4TTT0

I want to help, but can't find the place where I did this, if I come across it, I'll let you know.

what is your current code? did you switch to async Task?

Hello @Chad thank you for your willingness to help!

Yes I switched to async Task, this is my current code:

 <RadzenColumn Size="1" SizeSM="1" SizeMD="1">
         <RadzenButton Style="width: 100%" ButtonStyle="ButtonStyle.Primary" Icon="add" Variant="Variant.Flat" Click="@OnAddMatchPersonClick" />
     </RadzenColumn>
    <RadzenColumn Size="8" SizeSM="8" SizeMD="8">
         <RadzenDropDown @ref="this.radzenDropDown" Data="@matchPeopleForMatchPersonId" TextProperty="Name" ValueProperty="MatchPersonId" AllowClear=true
              Placeholder="Choose MatchPerson" style="display: block; width: 100%" @bind-Value=@matchFaceTemplate.MatchPersonId Name="MatchPersonId"/>
     </RadzenColumn>

code behind:

 private async Task OnAddMatchPersonClick()
 {
     MatchPerson matchPerson = await DialogService.OpenAsync<AddMatchPerson>("Add MatchPerson", null);
     if (matchPerson is not null)
     {
         this.radzenDropDown.Reset();
         this.matchFaceTemplate.MatchPersonId = matchPerson.MatchPersonId;
     }
 }

The idea is that the user can add a person to the list and then have the RadzenDropDown automatically select it.

Something to note is that I had to perform the .resest() on the component because otherwise the values in the list would not be updated

@P4TTT0 so you have it working now?

No :(, The new person it appears on the RadzenDropDown but it does not select automatically