How does asp.net MVC remember my false values on postback?
Posted
by Michel
on Stack Overflow
See other posts from Stack Overflow
or by Michel
Published on 2010-04-05T20:40:09Z
Indexed on
2010/04/05
20:43 UTC
Read the original article
Hit count: 217
asp.net-mvc
Hi,
This is working, but how???
I have a controller action for a post:
[AcceptVerbs(HttpVerbs.Post )]
public ActionResult Edit(Person person)
{
bool isvalid = ModelState.IsValid;
etc.
The Person object has a property BirthDate, type DateTime. When i enter some invalid data in the form, say 'blabla' which is obvious not a valid Datetime, it fills all the (other) Person properties with the correct data and the BirthDate property with a new blank DateTime. The bool isvalid has the value 'false'. So far so good.
Then i do this:
return View(p);
and in the view i have this:
<%= Html.TextBox("BirthDate", String.Format("{0:g}", Model.BirthDate)) %>
<%= Html.ValidationMessage("BirthDate", "*") %>
Ant there it comes: i EXPECTED the model to contain the new, blank DateTime because i didn't put any new data in. Second, when the View displays something, it must be a DateTime, because Model.BirthDate
can't hold anything but a DateTime.
But to my surprise, it shows a textbox with the 'blabla' value! (and the red * behind it)
Which ofcourse is nice because the user can seee what he typed wrong, but how can that (blabla)string be transferred to the View in a DateTime field?
© Stack Overflow or respective owner