I have a @foreach loop in my Blazor page which iterates through a list of type UserInput (var userInput in UserInput).
If userInput.IsInput is false, then I will display a
with contents userInput.Text.
If userInput.IsInput is true, I will display an field and store its contents in userInputBind[index].
Below is the code I have tried:
@foreach (var userInput in myList)
{
if (userInput.IsInput)
{
<input @bind="userInputBind[index]"/>
if (index < 6) index++;
}
else
{
<p>@item.Text</p>
}
}
@code {
public List<UserInput> myList = new List<UserInput>
{
new UserInput { IsInput = false, Text = "One" },
new UserInput { IsInput = false, Text = "Two" },
new UserInput { IsInput = true, Text = "" },
new UserInput { IsInput = false, Text = "Four" },
new UserInput { IsInput = true, Text = "" },
new UserInput { IsInput = false, Text = "Six" }
};
public List<string> userInputBind = new List<string> { "", "" };
public int index;
public class UserInput
{
public bool IsInput { get; set; }
public string Text { get; set; }
}
}
When I input "string1" into the first input field and "string2" into the second field, I expect the list to contain { "string1", "string2" }
. Instead, both of the strings are "string2". How can I fix this issue? Any help is appreciated!