I have table with one row of data with model binded, then i have an add button on clicking which im trying to clone the first row along with the model as well. But im not able to read all controls to clone and also please let me know how do i map the model to the dynamic controls, Note :- Model being binded is a List
<table id="Table11" align="center" style="width:95%" class="TFtable">
<tbody id="pettyTbody">
<tr>
@if (Model != null)
{
for (int i = 1; i < Model.pettyCashApprovalData.Count; i++)
{
<td></td>
<td>@Html.DropDownListFor(m => m.pettyCashApprovalData[i].AccountCode, new SelectList(new[] { "--Select AccNo--", "ACN001", "ACN002", "ACN003", "ACN004", "ACN005", "ACN006" }), new { @class = "drAccNo chosen-select" })</td>
<td>@Html.DropDownListFor(m => m.pettyCashApprovalData[i].Desciption, new SelectList(new[] { "--Select Decscription--", "Stationary Expense" }), new { @class = "drDescription chosen-select" })</td>
<td>@Html.DropDownListFor(m => m.pettyCashApprovalData[i].Expenses, new SelectList(new[] { "Select Some Options", "EMP01", "EMP02", "EMP03", "EMP04", "EMP05", "EMP06" }), new { @class = "drExpences chosen-select", @id = "drExpences", @multiple = "multiple", @style = "width:150px" })</td>
<td>@Html.TextAreaFor(m => m.pettyCashApprovalData[i].DOE, new { @style = "width:140px;", @class = "txtDOE txtbox " })</td>
<td>@Html.TextBoxFor(m => m.pettyCashApprovalData[i].ExpDate, String.Empty, new { @style = "width:90px;", @class = "txtExpDate txtbox " }) </td>
<td>@Html.TextBoxFor(m => m.pettyCashApprovalData[i].Amount, string.Empty, new { @style = "width:80px;", @class = "txtbox txtAmount" })</td>
<td>@Html.TextBoxFor(m => m.pettyCashApprovalData[i].sGVNO, string.Empty, new { @style = "width:100px;", @class = "txtbox txtwidth" })</td>
<td>@Html.TextBoxFor(m => m.pettyCashApprovalData[i].Remark, String.Empty, new { @style = "width:150px;", @class = "txtbox" })</td>
<td></td>
}
}
else
{
<td></td>
<td>@Html.DropDownListFor(m => m.pettyCashApprovalData[0].AccountCode, new SelectList(new[] { "--Select AccNo--", "ACN001", "ACN002", "ACN003", "ACN004", "ACN005", "ACN006" }), new { @class = "drAccNo chosen-select", @id = "AccountCode" })</td>
<td>@Html.DropDownListFor(m => m.pettyCashApprovalData[0].Desciption, new SelectList(new[] { "--Select Decscription--", "Stationary Expense" }), new { @class = "drDescription chosen-select", @id = "Expenses" })</td>
<td>@Html.DropDownListFor(m => m.pettyCashApprovalData[0].Expenses, new SelectList(new[] { "Select Some Options", "EMP01", "EMP02", "EMP03", "EMP04", "EMP05", "EMP06" }), new { @class = "drExpences chosen-select", @id = "drExpences", @multiple = "multiple", @style = "width:150px" })</td>
<td>@Html.TextAreaFor(m => m.pettyCashApprovalData[0].DOE, new { @style = "width:140px;", @class = "txtDOE txtbox " , @id = "DOE" })</td>
<td>@Html.TextBoxFor(m => m.pettyCashApprovalData[0].ExpDate, String.Empty, new { @style = "width:90px;", @class = "txtExpDate txtbox ", @id = "expDate" }) </td>
<td>@Html.TextBoxFor(m => m.pettyCashApprovalData[0].Amount, string.Empty, new { @style = "width:80px;", @class = "txtbox txtAmount" , @id = "Amount" })</td>
<td>@Html.TextBoxFor(m => m.pettyCashApprovalData[0].sGVNO, string.Empty, new { @style = "width:100px;", @class = "txtbox txtwidth", @id = "sGvno" })</td>
<td>@Html.TextBoxFor(m => m.pettyCashApprovalData[0].Remark, String.Empty, new { @style = "width:150px;", @class = "txtbox", @id = "Remark" })</td>
<td></td>
}
</tr>
</tbody>
Function to add rows dynamically by cloning
$("#anc_add").click(function () {
var count = $('#pettyTbody>tr').length;
alert(count);
$("#pettyTbody tr:first").clone().find("td").each(function () {
$(this).attr({
'id': function(_, id) { return id + count}
//name': function(_, name) { return name + count},
//'value': ''
});
}).end().appendTo("#pettyTbody");
i++;
});