I have a table template and table as below in MVC view
Template for dynamic row
<table id="Newrelation" style="display:none">
<tr>
<td>
<select id="relation-%" style="width:100px" class="relation_type" name="survey_detail[#].relation" value></select>
</td>
<td>
<select id="agegroup-%" style="width:100px" class="age_group" name="survey_detail[#].age_group" value></select>
</td>
<td>
<select id="nodependent-%" style="width:100px" class="no_dependent" name="survey_detail[#].no_dependent" value></select>
<input type="hidden" id="new_relation" name="survey_detail.Index" value="%" />
</td>
<td><input id="delete" class="delete" value="X" type="button"></td>
</tr>
</table>
Actual table
<table id="relation" class="table table-striped table-hover table-bordered">
<tbody>
<tr>
<th style="width:100px">
Relation
</th>
<th style="width:150px">
Age Group
</th>
<th style="width:150px">
No: Dependent
</th>
</tr>
@if (Model != null)
{
for (int i = 0; i < Model.survey_detail.Count; i++)
{
<tr>
<td>
@Html.DropDownListFor(m => m.survey_detail[i].relation, (SelectList)@ViewBag.Relation,"--", new { @class = "m-wrap" })
</td>
<td>
@Html.DropDownListFor(m => m.survey_detail[i].age_group, (SelectList)@ViewBag.Agegroup,"--", new { @class = "m-wrap" })
</td>
<td>
@Html.DropDownListFor(m => m.survey_detail[i].no_dependent, (SelectList)@ViewBag.Number, "--", new { @class = "m-wrap" })
<input type="hidden" name="survey_detail.Index" value="@i" />
</td>
<td><input id="delete" class="delete" value="X" type="button"></td>
</tr>
}
}
</tbody>
<table>
Jquery to attach new row on button click
$('#add_rel').click(function (e) {
var body = $('#relation').children('tbody').first();
var index = (new Date()).getTime();
var clone = $('#Newrelation').clone();
clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']'));
clone.html($(clone).html().replace(/"%"/g, '"' + index + '"'));
clone.html($(clone).html().replace(/"relation-%"/g, 'relation-' + index));
clone.html($(clone).html().replace(/"agegroup-%"/g, 'agegroup-' + index));
clone.html($(clone).html().replace(/"nodependent-%"/g, 'nodependent-' + index));
var newrow1 = clone.find('tr');
body.append(newrow1);
});
And it attach new row to the table but all the select list comes blank eventhough the @ViewBag.Relation,@ViewBag.agegroup,@ViewBag.Number
has data.
Please guide me to get values on the select list