Enum data binding not working

The documentation (Data Binding in Blazor MultiSelect Dropdown component - Syncfusion) doesn't show how to use an ENUM as a bind-Value and I am not able to figure out how to accomplish that.

I modified the default Blazor WASM client-server template as follows:

1. Add a property to the WeatherForecast.cs
public IEnumerable DaysOfWeek { get; set; }

2. Add the enum values to the code behind class FetchData.razor.cs
public string[] EnumValues = Enum.GetNames(typeof(System.DayOfWeek));
public List GridData { get; set; }

3. Added a Multiselect Dropdown to the page FetchData.razor


var wf = (context as WeatherForecast);

SfMultiSelect @bind-Value="@(wf.DaysOfWeek)" DataSource="@EnumValues" Mode="@VisualMode.Box" Placeholder="Select Day of Week"                        
               
Full FetchData.razor:
https://gist.github.com/DanielEggers/b98c55929928847bf7e0edeffcfddc2d

I am able to select multiple days but afterwards (on Save) the DaysOfWeek property of the modified object is null. 

PS: Why is it not possible to paste code snippets in here or attach code files?


3 Replies

AS ashimaz June 13, 2021 12:52 PM UTC

Hi, hope this helps... Change IEnumerable to string[], following code works.


<SfMultiSelect @bind-Value="@DaysOfWeek" DataSource="@EnumValues" Mode="@VisualMode.Box" Placeholder="Select Day of Week" ></SfMultiSelect>

<button @onclick="Save">Save</button>

@code {
    public string[] DaysOfWeek { get; set; }

    public string[] EnumValues = Enum.GetNames(typeof(System.DayOfWeek));

    private void Save()
    {
        Console.WriteLine(DaysOfWeek.Length);
    }

}


DA Daniel June 13, 2021 01:35 PM UTC

Thanks, that works. Even List is possible.
It is justa little bit sad, that IEnumerable ist not working.


AS ashimaz June 13, 2021 01:40 PM UTC

Welcome. I recommend using List<T> in such situations. Since items are added/removed based on selection.

enter image description here

Loader.
Up arrow icon