@page "/" <h3>Dynamic form</h3> <EditForm Model="@DataContext"> @foreach (var field in FieldIdentifiers) { if (field.Value == "string") { @field.Key @CreateComponent(field.Key); <br /> } } <h3>Statically declared</h3> <InputText @bind-Value="@DataContext.Name"></InputText> <br> Name: @DataContext.Name </EditForm> @code { [Parameter] public DataX DataContext { get; set; } = new DataX(); [Parameter] public Dictionary<string, string> FieldIdentifiers { get; set; } = new Dictionary<string, string> { { "Name", "string" }, { "Phone", "string" } }; public RenderFragment CreateComponent(string fld) => builder => { builder.OpenComponent(0, typeof(InputText)); // Get the initial property value var propInfoValue = typeof(DataX).GetProperty(fld); var s = propInfoValue.GetValue(DataContext); builder.AddAttribute(1, "Value", s); // Create the handler for ValueChanged. I use reflection to the value. builder.AddAttribute(3, "ValueChanged", Microsoft.AspNetCore.Components.RuntimeHelpers.TypeCheck<Microsoft.AspNetCore.Components.EventCallback<System.String>>(Microsoft.AspNetCore.Components.EventCallback.Factory.Create<System.String>(this, Microsoft.AspNetCore.Components.EventCallback.Factory.CreateInferred(this, __value => propInfoValue.SetValue(DataContext, __value), (string)propInfoValue.GetValue(DataContext))))); // Create an expression to set the ValueExpression-attribute. var constant = System.Linq.Expressions.Expression.Constant(DataContext, typeof(DataX)); var exp = System.Linq.Expressions.MemberExpression.Property(constant, fld); var lamb = System.Linq.Expressions.Expression.Lambda<Func<string>>(exp); builder.AddAttribute(4, "ValueExpression", lamb); builder.CloseComponent(); }; }
namespace BlazorFiddleProject { using Microsoft.AspNetCore.Components.Builder; using Microsoft.Extensions.DependencyInjection; public class DataX { public string Name { get; set; } = "Jane Doe"; public string Phone { get; set; } = "123456789"; public string Address { get; set; } = "The Street"; } public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IComponentsApplicationBuilder app) { app.AddComponent<App>("app"); } } }
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width"> <title>BlazorFiddleProject</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <style> app { } </style> <script type="text/javascript"> </script> </head> <body> <app>Loading...</app> <script src="_framework/blazor.webassembly.js"></script> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> </body> </html>

Add component

BlazorFiddle was updated from Blazor 0.7 to .NET 6.0. Your old source code could not work. You need to upgrade to latest.