Maybe I'm missing something but when I have a form that posts back to the same action, the textbox value reverts to the old value. The following example should increment the value in the textbox on each POST. This does not happen, the value on the model is incremented and the model is valid.
IF however I clear the modelstate in the HttpPost Action (the comment in the code), everything works as expected.
Am I missing something?
Here's the code:
Model:
public class MyModel
{
public int MyData { get; set; }
}
View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.MyModel>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) {%>
<%: Html.TextBoxFor(m => m.MyData)%> (<%: Model.MyData%>)
<%: Html.ValidationMessageFor(m => m.MyData) %> <br />
State :<%: ViewData["State"] %> <br />
<input type="submit" />
<% } %>
</asp:Content>
Controller:
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View(new MyModel { MyData = 0 });
}
[HttpPost]
public ActionResult Index(MyModel myModel)
{
// ModelState.Clear();
ViewData["State"] = "invalid";
if (ModelState.IsValid)
ViewData["State"] = "Valid";
var model = new MyModel { MyData = myModel.MyData + 1 };
return View(model);
}
}