I need to pass a list, based of of user input, from my view to the controller. I created a table using javascript, using a hidden input in each row incrementing the index in the id and name attributes as the user adds to the list. The issue is, I can not figure out how to decrement the index of each id and name if the user chooses to remove an element. I don't know much javascript... I hope you can understend my explanation.
This is my table.
<table id="cat-table" class="table table-responsive">
<thead>
<tr>
<th>Num Control</th>
<th>Usuario</th>
<th></th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
This is how I print each row.
$("#tbody").append("<tr id='filaUsuario" + usuario.id + "' ><td><input name='Usuario[" + i + "].UsuarioId' type='hidden' id='Usuario[" + i + "].UsuarioId' type='hidden' value='" + usuario.id + "' />" + usuario.id + "</td><td>" + usuario.name + "</td><td> <button type='button' class='btn btn-default' name = 'btn-Quitar' id = 'btn-Quitar' onclick= eliminar('" + i + "','"+ usuario.id +"') value='" + i + "'>Quitar</button></td></tr>");
And this is how I try to decrement the remaining elements
function eliminar(valor, usuario) {
//$("#filaUsuario" + usuario).remove();
$("#tbody tr").find("td:first").find("input").removeAttr("id");
$("#tbody tr").find("td:first").find("input").attr("id", "Usuario['"+
--i +"'].UsuarioId");
}