Binding complex type to Dropdown with Multiple

Hello. I have a class with a property of type IEnumerable of another class. It seems I'm able to bind this, but then whenever I unselect a dropdown option, I get a NullReferenceException

<RadzenDropDown AllowClear="true" @bind-Value="@employee.Roles" 
Multiple="true" Placeholder="( Select )" Data="@_roles"
TextProperty="@nameof(Role.Name)" ValueProperty="@nameof(Role)" />

employee in this instance is an Employee object:

public class Employee
    public IEnumerable<Role> Roles { get; set; }

and _roles is an IEnumerable<Role>

Hi @Relevant,

I’ve pushed today a fix for similar problem. You can get the source code and debug your case to see if it’s already fixed.

1 Like

Unfortunately that didn't seem to do it. The exception is being thrown at line 430 in DropDownBase SelectItem(). It seems it wasn't able to get the PropertyInfo. I'm assuming it's because I used the full type as the ValueProperty. So the ValueProperty is "Role" and so it's not able to find a property of Role named Role, because it's the class, not a property.

I'm unable to attach files, but here are the changes I made to DropDownPage.razor in order to test:

<h3 style="margin-top: 2rem">DropDown with multiple selection with complex data type</h3>
        <RadzenDropDown AllowClear="true" AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                        @bind-Value=@(employee.Roles) Multiple="true" Placeholder="Select..." Data=@roles TextProperty="Name" ValueProperty="Role" Style="width:400px" />

and in the code section

class Employee
        public IEnumerable<Role> Roles { get; set; }

    class Role
        public int Id { get; set; }
        public string Name { get; set; }

    Employee employee;
    IEnumerable<Role> roles;

    protected override void OnInitialized()
        roles = new List<Role>()
            new Role {Id = 1, Name="Role1"},
            new Role {Id = 2, Name="Role2"}

        employee = new Employee();
        employee.Roles = roles.Where(r => r.Id == 1);

There is no such property in your models

Yes, that is true. I was hoping to have the value be the entire object instead of just one property. Is that not possible? Would I need to instead have the bind-values be an IEnumerable<int> and then the ValueProperty be set to Id instead?

I originally had the bind-values set to an IEnumerable<int> but the problem I encountered is that when I tried to undo changes, I was not able to get the selections to revert back. Maybe this is a different topic and this one can be closed as working as intended and I can create a new post for that issue?

Okay, I got it figured out. I went back to binding it to the Ids of the navigation property. The issue wasn't in the DropDown control, but was in the entity framework entity that wasn't being reverted when I was trying to undo changes to it. Anyways. No issue with the dropdown, thank you for looking into it!