Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Before we test our model related updates, let’s make one more change to our controller.
Follow Along
To follow along, you’ll need to go back a step, as this video is a continuation of the previous video.
Additional Learning
The .NET Framework gives us a powerful set of tools for formatting DateTime values. See the following MSDN pages for more information.
-
0:00
Before we test our updates, let's make one more change to our Controller.
-
0:05
When processing a post request for
-
0:07
my add entry form, if we have a valid entry data model instance,
-
0:11
we need to save the entry and display the entry's list page back to the user.
-
0:16
Remember how the ModelState contained a collection of the request
-
0:19
form field values.
-
0:21
And for each of those values,
-
0:22
a collection of errors, given that the model state object is tracking all of
-
0:26
the form field value errors, it's able to tell us if our model is in a valid state.
-
0:31
We just need to add an if statement to check if the ModelStates
-
0:35
is valid property, is true.
-
0:40
If the model state is valid, we can save the entry data model instance
-
0:44
by calling the repositories at entry method.
-
0:47
And then we can display the entries list page
-
0:50
instead of redisplaying the AddEntry page.
-
0:56
We'll save doing that for the next video.
-
0:59
Let's move our breakpoint up to the beginning of the method and
-
1:02
press F5 to start our app.
-
1:07
The first thing that catches my eye is the date field value.
-
1:10
Now that we're using our entry data model and we've defaulted its date property
-
1:15
to today, we're getting both a date and a time.
-
1:19
This is happening because MVC calls the to string method on the date time value,
-
1:24
to get the textual representation of that value
-
1:27
when rendering the input elements value attribute.
-
1:30
The date time to string method returns a string containing both a date and a time.
-
1:36
We can customize the formatting of the value by using a different text box for
-
1:40
method overload.
-
1:42
The fifth overload accepts a format parameter,
-
1:45
which allows us to provide a string for the value format.
-
1:49
Behind the scenes, MVC is calling the string format method
-
1:53
with our provided format and the date field value.
-
1:57
Let's look at an example.
-
2:03
Var formattedDate = String.Format and
-
2:10
our format string and our value
-
2:15
model.Date and a semicolon.
-
2:21
If you haven't seen a format string before, it can look a little odd at first.
-
2:26
The format string is simply a string that contains one or more format items.
-
2:32
Format items are defined using a set of curly braces surrounding a number
-
2:37
that represents the index of the object whose string value will be inserted.
-
2:41
We're only working with one value here.
-
2:46
Our model date value.
-
2:48
So we'll use 0 for the index.
-
2:51
Then we can follow the index with a colon and a date time format.
-
2:57
I can never seem to remember the date time format strings that are available to use.
-
3:02
Let's open a new tab and search for
-
3:07
c#, standard, date, format, strings.
-
3:14
The first result is the page that we're looking for, the standard date and
-
3:18
time format strings page on MSDN.
-
3:22
If we scroll down a bit,
-
3:24
we can see that there are a variety of standard format strings available.
-
3:28
A lowercase d that will give us a short date pattern, and
-
3:33
an upper case D that will give us a long date pattern, and so on.
-
3:38
You can see an example of the output here on the right.
-
3:42
I want a short date pattern, so I'm going to go with the lower case D.
-
3:46
You can also create custom date and time format strings.
-
3:49
See the teachers notes for more information.
-
3:53
Let's add our format to the text box for method call.
-
3:58
Curly braces with 0 for the index followed by a colon and
-
4:03
a lowercase d, for our short date pattern.
-
4:07
Then let's remove our temporary example.
-
4:09
Press Ctrl + S to save the View and
-
4:14
refresh the page to see how this affects our date value.
-
4:18
Great!
-
4:18
That remove the time portion just like we wanted.
-
4:21
Lets change the activity value to 1 duration to 23 and
-
4:27
Notes to this is my note and save the form.
-
4:37
Here we are at our break point.
-
4:39
If we expand the entry parameter in the locals window,
-
4:42
we can see that all of our values have flowed correctly to our data model.
-
4:49
We can also hover over the ModelState.IsValid property
-
4:52
to see that is currently true which is what we'd expect.
-
4:57
Press F10 twice to move down to the repositories AddEntry method call.
-
5:02
I'll press F10 one more time to execute the method call.
-
5:06
Now we're at the end of our IF block.
-
5:09
In a bit we will work on displaying our entries list page but
-
5:12
for now we're just going to see your AddEntry page again.
-
5:16
Press F5 to continue.
-
5:18
This time let's remove the duration field value and save the form.
-
5:27
We're at our break point again.
-
5:29
The ModelState.IsValid property is false, which is what we want it to be,
-
5:34
given that we didn't supply a duration field value.
-
5:37
Press F5 to continue, and we're back on our form.
-
5:41
Ideally there'd be a validation message here,
-
5:44
letting us know that we need to supply a duration field value.
-
5:47
We'll implement that later in this course.
-
5:50
Go ahead and stop the app.
-
5:53
If you're using GitHub, let's commit our changes.
-
5:58
Enter a commit message of,
-
6:01
Updated the "AddEntry" controller and
-
6:06
view to use the Entry data model and click the commit all button.
-
6:15
Let's wrap up this section by updating our Add action method
-
6:18
to display the entries list page after successfully adding an entry.
You need to sign up for Treehouse in order to download course files.
Sign up