I'm trying to display a table in razor that has delete buttons on each row and there is an add button in the toolbar. When I delete an item it calls an ajax POST that will remove the item from the list(model).
//Ajax
//The add function is basically the same
function DeleteFromGrid(index) {
var formData = $("#GridForm").serialize();
$.ajax({
type: "POST",
cache: false,
url: "People/DeleteFromGrid?index=" + index,
data: formData,
success: function (data) {
$("#Container").html(data);
}
});
}
//Controller
//The add function is very similar is just adds an item
Public Function DeleteFromGrid(viewModel As PeopleViewModel, index As Integer) As ActionResult
viewModel.People.RemoveAt(index)
Return PartialView("PeopleView", viewModel)
End Function
//Html-Razor
<table id="MultiRateGrid" class="DataGrid" style="width: 100%">
<tbody>
<tr>
<th>@Html.LabelFor(Function(model) model.People(0).Name)</th>
</tr>
@For i = 0 To Model.People.Count - 1
Dim j As Integer = i
@<tr>
<td>
@Html.TextBoxFor(Function(m) m.People(j).Name)
@Model.People(j).Name
</td>
<td>
<input type="button" onclick="@String.Format("DeleteFromGrid(({0})", j)" value="Delete"/>
</td>
</tr>
Next
</tbody>
</table>
My issue is if I delete the first item from the list, the viewModel is updated correctly by me checking with @Model.People(j).Name (and in the controller) but actually displays the old value in the TextboxFor. I think it might be something with binding due to it retaining the previous object bound to People(j) because it is a lambda.