i'm facing the problem of the view showing data that's not in the model, and the model containing data that isn't in the view.
Imagine the data model has a birth date field:
DateTime: BirthDate;
And the view lets the user enter a birth date:
Birth Date: 11/28/1973
And that date goes into the model via the controller and all is right with the world.
But the way that text got into the TextBox was not at once, first the user types a "1":
Birth Date: 1
i cannot parse a "1" as any sort of valid date. The controller can react to this turn of events, e.g.:
- it can leave the existing BirthDate in the model alone
- it can set it to some form of "empty"
But now we have a problem because the view shows:
- "1"
but the model contains
- 11/28/1973
We have a disconnect between what the model contains and what the view shows.
The view is supposed to represent what is in the model, but it doesn't. Instead it is showing text that it stored in a TextBox.Text property. How do i reconcile the disconnect between the view showing what the model contains, and the view showing what it actually needs to?
i need to store this value somewhere. i can't leave the "1" sitting in the TextBox's Text
property, and it can't be stored in the model. What to do?
What if the user made a typo:
Birth Date: z
The view shows:
- "z"
but the model contains
- 11/28/1973
Things get even more complicated when the view is nothing like the data model at all:
Birth Date: 6 days ago
My controller can parse this string and convert it into a date, e.g. 3/11/2010. The controller stores the date 3/11/2010 in the model. But now we have a discrepancy between the view and the model. The view shows:
- "6 days ago"
while the model contains:
- 3/11/2010
The user can enter other dates that i cannot recognize:
Birth Date: mercury was in the 7th house
i have no idea how to turn this into a date, and i really am not going to try. i'm just going to tell to user to enter in dates like a normal person. But the question is: how do i show the user text they typed?
The view is used by the controller to display what's in the model. In this case the model does not contain "mercury was in the 7th house" - it cannot: the model for a birth date is a DateTime
which cannot hold this string.
And when the user tries to hit save, and i check if the BirthDate is valid, i'm going to (correctly) look at the model, which contains:
3/11/2010
and assume that the user has entered in a proper date. Except that they didn't.