by softlion
23. March 2010 08:25
Imagine you have this View, Model and this Action:
<% using (Html.BeginForm("MyActionPost")) { %›
<%= Html.TextBoxFor(m => m.Price)%> <input type="submit" value="Save" />
‹% } %›
[HttpPost]
public ActionResult ProductEditSave(ProductModel product)
{
product.PublicPrice += 5m;
return View("ProductEditForm", product);
}
public class ProductModel { public int Price { get; set; } }
After a fist submit You'll expect to see "5" in the box, after a second you'll expect to see "10". Instead you'll always see "0".
Why ? This is by "design" in ASP.NET MVC ! The "ModelState" collection content, which contains originally posted values, takes precedence over the values passed in the Model object (product).
Reference and possible workarounds :
http://stackoverflow.com/questions/594600/possible-bug-in-asp-net-mvc-with-form-values-being-replaced/2498155#2498155