I have a problem that I just don't understand. I have a very simple model that has a list as a public member. Whenever my controller removes an element from the model during postback, the HTML helpers of TextBoxFor () do not see the changes. These helpers seem to cache something, but I can't put it on it.
Demo / Repro can be found here: http://broken.azurewebsites.net
Repro
- Go to http://broken.azurewebsites.net
- Note 4 column values filled with null values
- Click the "test" button to send the POST to the page where I delete the first item in the list.
- Note. The "real" values are correct and element 0 is deleted, however, the problem here is that the values are displayed through TextBoxFor (). I cannot understand why this is showing 0 when this element no longer exists.
Models
public class ItemViewModel { public string Description { get; set; } public decimal? Amount { get; set; } } public class TestViewModel { public TestViewModel() { Items = new List<ItemViewModel>(); } public List<ItemViewModel> Items { get; set; } }
controller
public class HomeController : Controller { public ActionResult Index() { var model = new TestViewModel(); for (var i = 0; i < 4; i++) { model.Items.Add(new ItemViewModel { Description = i.ToString(), Amount = i }); } return View(model); } [HttpPost] public ActionResult Index(TestViewModel model) { model.Items.RemoveAt(0); return View(model); } }
View
@model Demo.Models.TestViewModel @using (Html.BeginForm()) { <table> <thead> <tr><td>Description</td><td>Amount</td><td>Real-Description</td><td>Real-Amount</td></tr> </thead> <tbody> @for (var i = 0; i < Model.Items.Count; i++) { var ii = i; <tr> <td>@Html.TextBoxFor(m => m.Items[ii].Description)</td> <td>@Html.TextBoxFor(m => m.Items[ii].Amount)</td> <td>@Model.Items[ii].Description</td> <td>@Model.Items[ii].Amount</td> </tr> } </tbody> </table> <button>Test</button> }
Todd carter
source share