C#C
C#2y ago
gax

Generating input fields for each element in an array in Blazor WASM

I'm trying to generate an input field for each element in an array, and have the input field bind that element to the array, however, no matter how i do it, it either throws an exception or doesn't bind at all.

This is basically what I have right now

<div style="display: grid; grid-template-columns: repeat(3, 50px); gap: 10px;">
    @for (int i = 0; i < a.Length; i++)
    {
        <input type="text" style="width: 50px" placeholder="@i" @bind-value="a[i]" @key="i"/>
    }
</div>

@code {
  private string[] a = new string[9];
}

Currently, it generates the input fields, but when attempting to change values, it throws an IndexOutOfRangeException
Was this page helpful?