Two-way data binding does not work

I'm trying to bind a model property as two-way binding but it does not work for me
can anyone help me please.

<RadzenDropDown AllowFiltering="true" " Data="sectList" @bind-SelectedItem="car.SectionId" TextProperty="SectionName" ValueProperty="Id" />

@bind-SelectedItem returns object but I want two-way int property

SelectedItem is the entire data item, you need @bind-Value however it will be of type object as well since the DropDown can have multiple selection and in this case Value can be IEnumerable. You can cast to desired type. More info can be found here (check the source code):
https://blazor.radzen.com/dropdown

I tried to use @bind-Value before but it does not display the selected item I choose in the case of edit.
it stay empty after selection

Have you checked the demo?

Yes, I checked it and do like it but I bind value to a model int property
<RadzenDropDown AllowFiltering="true" AllowClear="true" @bind-Value="car.DepartId" Data="deptList" TextProperty="DepartName" ValueProperty="Id" />

@khaledalmolaa we will need a minimal reproduction of the issue you are describing.

when I bind it to local variable it select item just one time and can't change after that

for information 'DepartId' is an int type property

@khaledalmolaa thanks! We will update the thread when there is progress.

Issue confirmed and fixed immediately! You can get Radzen.Blazor 1.0.2 from NuGet. DropDown examples also updated: https://blazor.radzen.com/dropdown

Thank you very much, it's works like a charm.
I really appreciate your efforts :slight_smile:

@enchev, last change has generated a new bug, please try example https://blazor.radzen.com/dropdown
option DropDown with multiple selection

Unfortunately you are right. Fix will be published later today.

I've fixed the demo to use IEnumerable<object> when binding DropDown with multiple selection. Thanks for the report!

Thanks to the whole team for the good work you are doing. :clap:

1 Like

Hi @enchev,

I have updated nuget from 1.0.9 to 1.0.12 and I have problems with DropDown with multiple selection. I have seen you have back to IEnumerable<string>. I have also change object to string but I have other problem with selectItem and selectall.

I'd like you to test this several cases. (I can't understand how DropDown works, yet)

In your example in method OnInitializedAsync:

a) EF with your database (I have added ToList)

customers = await Task.FromResult(dbContext.Customers.ToList());

b) without EF (one with ToList() and other without ToList())
b1) customers = await Task.FromResult(Enumerable.Range(0, 9).Select(x => new Customer { CustomerID = x < multipleValues.Count() ? multipleValues.ToList()[x] : x.ToString(), CompanyName = $"CompanyName-{x+1}" }));
b2) customers = await Task.FromResult(Enumerable.Range(0, 9).Select(x => new Customer { CustomerID = x < multipleValues.Count() ? multipleValues.ToList()[x] : x.ToString(), CompanyName = $"CompanyName-{x+1}" }).ToList());

This is definitely a bug and it is fixed immediately! Update of Radzen.Blazor was just published.

I'm not sure I understand the idea here. The DropDown will select items based on ValueProperty which in this case is CustomerID. Both selects will create multiple items with same CustomerID.

Hi @enchev,

I wanted to say that I proposed you to try the demo by changing the original line (customers = await Task.FromResult(dbContext.Customers;) for option a), b1) & b2) but only one option at the same time.

Bs Options are in order to test with some no EF solution.

thanks!

Yes, thanks again for the report! Now the DropDown will work normally not only with IQueryable but with other generic IEnumerable including arrays, lists, etc.

Hello @enchev,

I have tried version 1.1.0 and It works better, but I can't work in next case (multiple="true"):
fill customers with list,
"customers = await Task.FromResult(dbContext.Customers.ToList());"
and try to select all items

browser error message:
Error: System.NullReferenceException: Object reference not set to an instance of an object.

at Radzen.Blazor.RadzenDropDown`1.SelectAll()

at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[T](MulticastDelegate delegate, T arg)

at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg)

at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(UInt64 eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs)