Indexes are wrong if I use Array.splice with a knockout observableArray

Nov 18, 2014 at 2:15 PM
I am using Html.ClientBlockRepeater.
However, in javascript, when I modify the Knockout observableArray that the ClientBlockRepeater is bound to, the indexes start to be wrong.

For example, the HTML may start out as something like this:

<div data-name-prefix="Items[0]">
<input name="Items[0].Value" />
</div>
<div data-name-prefix="Items[1]">
<input name="Items[1].Value" />
</div>

Then I use "splice()" in order to replace an item in the array. For example:

window.theModel.Items.splice(0,1,newItem);

Then the HTML looks like this:

<div data-name-prefix="Items[2]">
<input name="Items[2].Value" />
</div>
<div data-name-prefix="Items[1]">
<input name="Items[1].Value" />
</div>

The indexes in HTML should be "0", not "2". Incidentally, this no longer posts to my page since the MVC mapper expects arrays to start at "0".


I did some initial investigation, and it look like there is a "_tag" property that MvcControlToolkit.Bindings-3.0.0.js appends to things. This "_tag" property is used as the index. This property is generated using mvcct.utils.uniqueTags.newTag(). I don't think this is correct, though.