I have an issue which I really just don't understand. I have a very simple model which has a List as a public member. Whenever my controller removes an element from the model on postback the TextBoxFor() HTML helpers do not seem to pick up the changes. These helpers appear to be caching something but I cannot put my finger on it.
A Demo/Repro can be found here: http://broken.azurewebsites.net
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>
}
Change your post index method as below for the desired behavior:
[HttpPost]
public ActionResult Index(TestViewModel model)
{
ModelState.Clear();
model.Items.RemoveAt(0);
return View(model);
}
Here's why:
The TextBoxFor binds to the post value in the ModelState instead of model value. So in your application, when you hit Test button, the text boxes are bound to the value they already have and don't get updated on postback even after the model values are changed . For ex, If the text boxes in first row are showing 0 and they will remain bound to that value even after post back and the underlying model is having value 1. The reason behind this behavior is validation. If you are expecting an int in the text box and user inputs "SomeTextValue",the model binder won't be able to bind to the int property and it will be in a state of validation error. You then would want the user to see an error that says "SomeTextValue" is not an integer. Please enter an integer because you'd expect the user entered value to be there.
Rick Strahl explains it perfectly in this blog post
http://www.west-wind.com/weblog/posts/2012/Apr/20/ASPNET-MVC-Postbacks-and-HtmlHelper-Controls-ignoring-Model-Changes
And Brand Wilson below:
http://forums.asp.net/post/3688022.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With