I have an EditorFor template that binds to a list and creates html elements with name attributes that represents the elements in the array, e.g.
<input id="Contacts_0__Uid" name="Contacts[0].Uid" type="hidden"
value="sdfsdfs6-f1a1-44e4-af30-9cfb00a8d654">
<input id="Contacts_1__Uid" name="Contacts[1].Uid" type="hidden"
value="f9826696-f1a1-44e4-af30-9cfb00a8d654">
<input id="Contacts_2__Uid" name="Contacts[2].Uid" type="hidden"
value="23423skf-f1a1-44e4-af30-9cfb00a8d654">
I have a piece of javascript that deletes a row from a table. The upshot of this is when the form gets posted to the server, the array indices of the list are out of sync, e.g:
<input id="Contacts_1__Uid" name="Contacts[1].Uid" type="hidden" value="f9826696-f1a1-44e4-af30-9cfb00a8d654">
<input id="Contacts_3__Uid" name="Contacts[2].Uid" type="hidden" value="23423skf-f1a1-44e4-af30-9cfb00a8d654">
There is no element index 0. It seems like the modelbinder is ignoring this list when the indices are out of sync.
I know I could maybe write some javascript to change the name 开发者_如何学Goattributes but I am wondering if there is a way round this on the server?
Very late answer, but it could help somebody else down the line.
Do not remove the DOM elements (rows) with javascript. Instead, mark them to be deleted with a custom attribute like "destroy" and hide them from view. This way, they will be submitted back to server and your array will remain intact.
Then, serverside, you can evaluate the whole array, check all elements for "destroy" attributes and execute the actual removal logic before persisting the object graph.
精彩评论