I have an ASP.NET 4.5.6 MVC 5 app. Here's the summary of my models/views/controllers:
Model classes
public class ViewModelA {
public ViewModelB CurrentItem { get; set; }
// ...
}
public class ViewModelB {
public List<Parameter> Parameters { get; set; }
// ...
}
public class Parameter {
public string Name { get; set; }
public int Position { get; set; }
// ...
}
View
@model ViewModelA
@* ... *@
@for (int i = 0; i < Model.CurrentItem.Parameters.Count; i++) {
@Html.EditorFor(m => m.CurrentItem.Parameters[i])
}
~/Views/Shared/EditorTemplates/Parameter.cshtml
@model Parameter
<tr>
<td class="col-md-3">
</td>
<td class="col-md-9">
@Html.TextBoxFor(m => m.Position)
@Html.TextBoxFor(m => m.Name)
@* Model.Position always equals list index + 1 *@
<button type="submit" class="btn btn-default" formaction="@Url.Action("DeleteParameter", "MyController", new { deleteIndex = (Model.Position - 1) })" for formmethod="post" title="Delete parameter">
<i class="glyphicon glyphicon-minus"></i>
</button>
<!-- Debug only -->
<span>Position = @Model.Position | Name = @Model.Name</span>
</td>
</tr>
Controller actions
[HttpPost]
public ActionResult AddParameter(ViewModelA viewModel) {
var p = new Parameter() {
Name = "",
Position = (byte)(viewModel.CurrentItem.Parameters.Count + 1)
//...
};
viewModel.CurrentItem.Parameters.Add(p);
return View("Edit", viewModel);
}
[HttpPost]
public ActionResult DeleteParameter(int deleteIndex, ViewModelA viewModel) {
viewModel.CurrentItem.Parameters.RemoveAt(deleteIndex);
for (int i = deleteIndex; i < viewModel.CurrentItem.Parameters.Count; i++) {
viewModel.CurrentItem.Parameters[i].Position--;
}
return View("Edit", viewModel);
}
Here is an image that shows a series of screenshots after adding a couple of parameters, then deleting one. Horizontal red lines separate each screen after refresh.
After deleting the parameter in position 1, I would expect the remaining parameter to have the name "Name2", but it still shows "Name1" (even though the "debug span text" shows the right name). The controller and model seem to be doing what I expect. Why is the textbox in the view showing the wrong name?