I am new to MVC and JavaScript, so forgive my basic questions. I am writing a view that displays several rows of data and at the end of each row I am displaying a Remove button. When clicked, the button runs some JavaScript that removes the row using the .remove().
When I click the Remove button, the row is removed, but when I return to the controller, any rows below the row that was deleted is not transferred back to the controller and the count is one less then the row I deleted. For example if I have 6 rows and delete row 4, when I submit back to the controller, the count is 3 and rows 5 and 6 are missing. Not sure why. Below is my View and controller.
Any help will be greatly appreciated.
View
@using (Html.BeginForm("Edit", "Contacts", FormMethod.Post))
{
<table>
<tr>
<th><label>Last Name</label></th>
<th><label>First Name</label></th>
.... // more table headings
<th></th>
</tr>
@if (Model != null && Model.Count > 0)
{
int j = 0;
foreach (var i in Model)
{
<tr>
<td>
@Html.TextBoxFor(a => a[j].LASTNAME)
@Html.HiddenFor(a => a[j].DOB_NBR)
@Html.ValidationMessageFor(a => a[j].LASTNAME)
</td>
<td>
@Html.TextBoxFor(a => a[j].FIRSTNAME)
@Html.ValidationMessageFor(a => a[j].FIRSTNAME)
</td>
.... // more controls
<td><button type="button" id="btnDelete" class="deleteContact btn btn btn-danger btn-xs">Remove</button></td>
</tr>
j++;
}
}
</table>
<input type="button" class="add-button btn btn-default" name="add" value="Add" />
<input type="submit" name="submitAction" value="Submit" class="btn btn-primary" onclick="return confirm('Please double check all information before submitting. Submit Notification?')" />
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
$(document).on("click", ".deleteContact", function () {
$(this).closest("tr").remove();
});
</script>
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(string submitAction, List<ENTITY_CONTACTS> entity_Contacts)
{
// entity_Contacts is not bound with all items in the collection
}