@page "/" @for (int i = 0; i < foos.Count; i++) { int iCopy = i; var foo = foos.GetFoos()[i]; <div class="col-3"> <div class="btn-group" role="group" aria-label="Basic example"> <button type="button" class="btn btn-primary" @onclick="@(() => InsertFooAt(iCopy))">Insert above</button> <button type="button" class="btn btn-primary" @onclick="@(() => RemoveFooAt(iCopy))">Delete</button> <button type="button" class="btn btn-primary" @onclick="@(() => InsertFooAt(iCopy+1))">InsertBelow</button> </div> </div> <div class="col-3"> <div class="btn-group" role="group" aria-label="Basic example"> <button type="button" class="btn btn-primary" @onclick="@(() => InsertFooAtAsync(iCopy))">Insert above async</button> <button type="button" class="btn btn-primary" @onclick="@(( ) => RemoveFooAtAsync(iCopy))">Delete async</button> <button type="button" class="btn btn-primary" @onclick="@(() => InsertFooAtAsync(iCopy+1))">InsertBelow async</button> </div> </div> <div class="col-6"> <input type="text" class="form-control" @bind-value="@foo.Data"/> </div> } @if (foos.Result == null) { <div class="spinner-border" role="status"> <span class="visually-hidden">Loading...</span> </div> } else { <span class="border-success">@foos.Result</span> } @code { FooCollection foos = new FooCollection(); void InsertFooAt(int index) { foos.CreateFooAt(index); } void RemoveFooAt(int index) { foos.RemoveFooAt(index); } async Task InsertFooAtAsync(int index) { await foos.CreateFooAtAsync(index); await InvokeAsync(StateHasChanged); } async Task RemoveFooAtAsync(int index) { await foos.RemoveFooAtAsync(index); await InvokeAsync(StateHasChanged); } public class Foo { public string Data { get; set; } public Foo() { Data = "bar"; } } public class FooCollection { public int Count => foos.Count; public string? Result { get; private set; } private List<Foo> foos; public FooCollection() { foos = new List<Foo>() { new Foo() }; Result = foos.Single().Data; } public IList<Foo> GetFoos() { return foos; } public void CreateFooAt(int index) { foos.Insert(index, new Foo()); UpdateResult(); } public void RemoveFooAt(int index) { foos.RemoveAt(index); UpdateResult(); } public async Task CreateFooAtAsync(int index) { foos.Insert(index, new Foo()); await UpdateResultAsync(); } public async Task RemoveFooAtAsync(int index) { foos.RemoveAt(index); await UpdateResultAsync(); } private void UpdateResult() { Result = null; Thread.Sleep(1000); // Do some expensive operation on the data Result = foos.Select(f => f.Data).Aggregate((a, b) => a + ", " + b); } private async Task UpdateResultAsync() { Result = null; await Task.Delay(1000); // Do some expensive operation on the data Result = foos.Select(f => f.Data).Aggregate((a, b) => a + ", " + b); } } }
namespace BlazorFiddleProject { using Microsoft.Extensions.DependencyInjection; public class Startup { public void ConfigureServices(IServiceCollection services) { } // Warning: IComponentsApplicationBuilder - not the original .NET 6.0 interface. This was done to make old saves work. 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"> <link href="_content/MatBlazor/dist/matBlazor.css" rel="stylesheet" /> <script src="_content/MatBlazor/dist/matBlazor.js"></script> <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.