Spent a bit of time coming up with a suitable title, I'm not sure if I worded it correctly. For my example, I will use a Person object, but my real application is using something more domain-specific:
public class Person {
public string Name { get; set;}
public string Gender { get; set; }
public string HairColor{ get; set; }
}
Basically, my requirements are to allow the user can input 0 - 10 Person objects, which will map to a List<Person> People
property in the model.
The way I was thinking about doing this is having a block of inputs related to a single Person:
<div>
<label for="name">Name</label>
<input type="text" name="name">
</div>
<div>
<label for="gender">Gender</label>
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
</div>
<div>
<label for="hairColor">Hair Color</label>
<input type="radio" name="hairColor" value="black"> Black
<input type="radio" name="hairColor" value="brown"> Brown
<input type="radio" name="hairColor" value="blonde"> Blonde
<input type="radio" name="hairColor" value="red"> Red
</div>
with a "Add another" button on the bottom, which will append an additional block of these inputs using JQuery.
My question is, how would I generate the name
attribute in the html input tags to bind to the List<Person>
properly? Would <input type="text" name="People[0].Name">
work?