I have a form containing a list of variable length text fields created using a template like this.
@Html.TextBox("items[" + itemIndex + "].Title", someValue)
So, the final rendered HTML looks something like this:
<input id="items_0__Amount" type="text" value="Apple" name="items[0].Title">
<input id="items_1__Amount" type="text" value="Banana" name="items[1].Title">
<input id="items_2__Amount" type="text" value="Orange" name="items[2].Title">
When submitting the form, this binds to my model just fine. However, I have a delete button that uses Javascript to remove one or more lines from the form. The problem is that if you delete, say, the middle line, the HTML looks like this:
<input id="items_0__Amount" type="text" value="Apple" name="items[0].Title">
<input id="items_2__Amount" type="text" value="Orange" name="items[2].Title">
... and the indices are no longer adjacent. This seems to confuse MVC, and my model binder only passes the first line, not the last. Am I doing something wrong, or does MVC just fail if the indices in the lists are not contiguous? What is the best solution to this problem?
JS , .
!