First of all I am trying to Post a list via form. It looks like this :
<div class="cnl-box-otr">
<div class="form-group">
<label>Name</label>
@Html.TextBoxFor(m => m.list[0].name, new { @class = "form-control", placeholder = "Name" })
</div>
<div class="form-group">
<label>Percentage</label>
@Html.TextBoxFor(m => m.list[0].percentage, new { @class = "form-control", placeholder = "Percentage" })
</div>
</div>
Now i create Other dynamic Same div with jquery like this on a click of button:
$("#AddMorediv").on('click', function () {
var index = parseInt($('#index').val());
index = index + 1;
var contentToBeInserted = '<div class="cnl-box-otr" id="row"><a href="#" class="cnl-box DeleteRow"><i class="fa fa-times"></i></a><div class="form-group"><label>Details</label><input class="form-control" id="list_' + index + '_Name" name="list[' + index + '].Name" placeholder=" Name" type="text" value=""></div><div class="form-group"><label>%</label><input class="form-control" data-val="true" id="list' + index + '_Percentage" name="list[' + index + '].Percentage" placeholder="Percentage" type="text" value=""></div></div>'
$('.writericondiv').append(contentToBeInserted);
$('#indexwriters').val(index);
});
This Creates my list perfectly and Posted successfully as a list. But the problem is i have also given an option to remove these divs one by one. I f i try to remove the 1st index and then post the form. Only zero index value is posted successfully to the controller but greater than 1st index does not Post its value. The list looks like this while posting.
<div class="cnl-box-otr">
<div class="form-group">
<label>Name</label>
@Html.TextBoxFor(m => m.list[0].name, new { @class = "form-control", placeholder = "Name" })
</div>
<div class="form-group">
<label>Percentage</label>
@Html.TextBoxFor(m => m.list[0].percentage, new { @class = "form-control", placeholder = "Percentage" })
</div>
</div>
<div class="cnl-box-otr">
<div class="form-group">
<label>Name</label>
@Html.TextBoxFor(m => m.list[2].name, new { @class = "form-control", placeholder = "Name" })
</div>
<div class="form-group">
<label>Percentage</label>
@Html.TextBoxFor(m => m.list[2].percentage, new { @class = "form-control", placeholder = "Percentage" })
</div>
</div>
Hope You understand what i am trying to ask. If not, Kindly Add a Comment i will definitely improve this Question.
Thanks for any help or suggestions.