In my view I have the following code:
@using (Html.BeginForm()) {
@Html.TextBox("Date2", Model.Date2)
<br/>
@Model.Date2
<input type="submit" value="Create" />
}
In my controller I have :
[HttpPost]
public ActionResult Index(Home home){
ViewBag.Message = "Welcome to ASP.NET MVC! (after a submit)";
home.Date2 = home.Date2.AddDays(1);
return View("Index", home);
}
The model is:
public class Home {
public DateTime Date2 { get; set; }
}
My question is : Why when I hit the submit button than the TextBox display not the date with 1 more day and the html that I output directly the date is?
Edit:
If I step into MVC3 source code I can see that private static MvcHtmlString InputHelper(...) get the value parameter with the good date but later the code get the old value from attemptedValue… You can see the debug here https://i.stack.imgur.com/x2mTI.png
ModelStatesaves a copy of the value input into the form field. Usually, if you’re displaying a form in aPOSTaction it’s because validation has failed, so you want to re-display the “attempted” value input rather than a new one (this is what the input HTML helpers do by default).To prevent it using the attempted value you could remove it from ModelState, e.g: