Search code examples
c#blazorblazor-client-side

Blazor Radzen dropdown on primitive type?


I have a Blazor application where I am using the Radzen components. Specifically, I am using the drop down component. I am running into an issue where I am unable to bind the Data to a list of floats.

<RadzenDropDown @bind-Value="@model.modelValue" Data="options"></RadzenDropDown>

@code {
    List<float> options;
    options.Add(1);
    options.Add(2);
    ...
}

When I try this, I get the following error:

cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'

I know it is possible to do the following to make it work:

<RadzenDropDown @bind-Value="@model.modelValue" TextProperty="Text"  Data="@(options.Select(s => new { Text = s, Value = s }))"></RadzenDropDown>

I'd like to avoid that because I have an modal that allows the user to manually enter a value and I can't get that value to show if I have to convert my float value to an object.

Is there anyway to bind to primitives?


Solution

  • The component is looking for an IEnumerable<object>, not float. You can pass in a List<object> (since it implements IEnumerable), and then put floats in it, since float's are objects. Here's a full working example.

    @page "/radzentest"
    
    
    <RadzenDropDown TValue="float" Data="Options"></RadzenDropDown>
    
    @code {
    
        List<object> Options = new List<object>();
        protected override void OnInitialized()
        {
            Options.Add(1.1f);
            Options.Add(2.5f);
        }
    }
    

    Result:
    enter image description here

    I'm not sure if there is a technical reason why Radzen couldn't do it that I don't understand, but it seems like a miss on their part to not have it be IEnumerable<T> instead of IEnumerable<object>