I am trying to accomplish Functionality similar to Angular FormBuilder, where we dynamically add Controls and Validations.
In MudBlazor or Blazor in general, we have to use C# Model to bind with Controls.
So I decided to use ExpandoObject with dynamically added Properties to bind with MudBlazor controls with OnInitialized
Method as below
dynamic FormModel = new ExpandoObject();
protected override void OnInitialized()
{
FormModel.FirstName = null;
FormModel.LastName = null;
FormModel.Age = 0;
//Or with Dictionary
((IDictionary<string, object>)FormModel).Add("FirstName", "");
((IDictionary<string, object>)FormModel).Add("LastName", "");
((IDictionary<string, object>)FormModel).Add("Age", 0);
}
and in HTML using MudBlazor Textbox
<MudTextField Label="First name" @bind-Value=FormModel.FirstName />
While compiling with first approach it gives the following error
Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.
While running with second "object" approach it gives error at runtime, when value is changed/entered.
Conversion to type System.Object not implemented