Blazor binding a List<string> in an EditForm
Solution 1:
The answer is in the accepted answer you linked to...
You are looking to create a two-way data binding of a collection.
<EditForm Model="@person">
@foreach (var PName in person.names)
{
<InputText @bind-Value="@PName.Name" />
}
</EditForm>
@code
{
private Person person = new Person {ID = "1", names = new List<PersonName>()
{ new PersonName {Name = "Marry" },
new PersonName {Name = "Marria" }
};
public class Person
{
public string ID {get;set;}
public List<PersonName> names { get; set; }
}
public class PersonName
{
public string Name { get; set; }
}
}
Note that in order to bind the Name property, you must define it in a class of its own, and in the Person model you define a list of that class ( PersonName ). This is the only way to bind to a collection.