I have a table that its rows are created dynamically with jQuery. I can add or remove rows so when all fields are ready, I just submit it. Since the rows are created before any posting, how can I properly name them to later easily retrieve their values in controller actions like Create? Consider that each row is "an object" or at least an array for each one.
In ruby, just by adding [] to the name=
attribute do the job, not sure if ASP.NET is possible to that.
This is the script for creating rows:
function add_row() {
var detalle = $('#detail');
var index = $("#detail tbody").children("tr").length;
var newRow = '<tr>' +
'<td style="display:none"><input name="Detalle.Index" type="hidden" value="' + index + '" /></td>' +
'<td data-title="Quantity">' +
'<div class="input-group">' +
'<span class="input-group-addon"></span>' +
'<input class="form-control" type="number" aria-label="Quantity" name="Detail['+ index +'].quantity" id="Detail_' + index + '__quantity" step="0.001" min="0.000">' +
'</div>' +
'</td>' +
'<td data-title="Price" >' +
'<div class="input-group">' +
'<span class="input-group-addon">$</span>' +
'<input type="number" class="form-control" aria-label="Price" name="Detail['+ index +'].price" id="Detail_' + index + '__price" step="0.001" min="0.000" required="true">' +
'</div>' +
'</td>' +
'<td data-title=""><button type="button" class="btn btn-danger btn-sm" onclick="delete_row(this)">Cancel</button></td>' +
'</tr>';
detail.append(newRow);
}
In controller I can access to Request with Request.Form["Detail[0].price"]
to get a specific value, however I'm not able how to get the total of rows to create a loop and get values like this: Detail[i].inputname
Any suggestion will be appreciated.