I’m trying to create a form based on data from an AJAX request. Users should be able to add elements by selecting them and add those elements to a form that can be submitted. The elements should be added to a collection named Employees.
The elements are requested via AJAX from the database and have the primary key of the entity. When I look at the HTML generated by the framework helpers it looks kind of like this for the hidden fields with the Ids:
<input id="Employees_0__EmployeeId" name="Employees[0].EmployeeId" value="1" type="hidden">
Like I see the 0 is the index for the collection-element. How can I manage it to add and remove the elements dynamically the right way using the framework? Are there some helpers, that can support me on doing so?
If possible, I don't wanna code a workaround that manages the used indizes, to prevent overwriting an item for the collection.
UPDATE:
I tried the suggested solution but it fails, cuz it seems the index must be incremental, starting by 0:
var dummyindex = (new Date()).getTime();
var inputelement = $('<input data-val="true" id="Employees_'
+ dummyindex
+ '_EmployeeId" name="Employees['
+ dummyindex
+ '].EmployeeId" value="'
+ $(this).parent().attr('id') // this is the parent key
+ '" type="hidden">');