Normally, if we want to bind a list to our viewmodel we would need to do something like:
public class ViewModel
{
public List<SomeModel> MyList { get; set; }
}
@for (int i = 0; i < Model.MyList.Count; i++)
{
<input asp-for="MyList[i].SomeProp" ..>
}
Which gives elements with names like MyList[0].SomeProp
or MyList_0__SomeProp
.
But what if my list is empty at first and I want to fill it client-side (user can add as many elements as he wants).
Is it necessary that I maintain an index (that MUST start at 0) and whenever the user adds an element I name it like that? Thing is, if the user deletes an element then I have to re-order the indexs right?
Is there a way to bind to a list in our viewmodel without having to use indexing, some like MyList[].SomeProp
directly (even though I don't think the server would know how to split elements in this case).
Thank you.