Search code examples
asp.net-coreblazorexpandoobject

Binding to a ExpandoObject in Blazor


I've build a class which has a dynamic list of properties (such as color, size, weight etc etc), the contents of this aren't known at compile time so I used a ExpandoObject

Where I'm struggling is to get Blazor to bind to the value properties of the object for example

@foreach (var option in (IDictionary<string, object>)selectedProduct.Properties)
{
    <div class="col-sm">
        <input type="text" placeholder="@option.Key" @bind="option.Value"/>
    </div>
}

However when this is compiled the autogen code results in an error :- 'KeyValuePair<string, object>.Value' cannot be assigned to

Has anyone had any success binding to a ExpandoObject (and children properties?)


Solution

  • This is not directly related to the ExpandoObject.

    When you iterate over a Dictionary you get back immutable KeyValuePair items.

    You get an error because @bind="option.Value" compiles to code that wants to assign to option.Value.

    And as tuples and anonymous types are all immutable I think you will have to write a <Label, Value> class for this.