Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let’s start work on our “Edit Entry” page by updating our controller to handle updates.
Follow Along
To follow along commiting your changes to this course, you'll need to fork the aspnet-fitness-frog repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork>
cd aspnet-fitness-frog
git checkout tags/v5.1 -b updating-our-controller-to-handle-updates
Additional Learning
For more information on the DRY (Don’t Repeat Yourself) design principle, see this Wikipedia page.
For more information about access modifiers check out this page on MSDN.
Keyboard Shortcuts
-
CTRL+PERIOD
- Opens the quick actions popup -
CTRL+SHIFT+B
- Build solution
[MUSIC]
0:00
In this section, we'll be updating the
fitness frog web app with the ability to
0:04
update and delete entries.
0:09
Once those features are in place,
our web app will be completed.
0:11
In this video, we'll be implementing the
ability to update entries The Edit Entry
0:16
page, as you're about to see,
is very similar to our Add Entry page.
0:21
Let's see how this will allow us to
share code between the two pages.
0:26
Here's our EntriesController class.
0:30
If we navigate to the EditAction method.
0:32
We can see that aside from checking
if the ID parameter is null
0:35
it's currently just
returning the Edit View.
0:39
Let's add some TODO comments to plan
out the changes that we need to make.
0:42
First, we need to update this Action
Method to make a call into the repository
0:46
to get the entry for
the provided ID parameter value to do
0:51
get the requested entry
from the repository.
0:56
But also make sure that an entry was found
in if not return a status of not found.
1:03
TODO Return a status of "not found"
1:08
if the entry wasn't found.
1:15
Lastly, we need to pass the entry to our
view.TODO Pass that entry into the view.
1:21
We also need to add a corresponding
Post action method, just like we did for
1:30
the ad entry page.
1:34
I'll go ahead and
stub out that method now.
1:36
Public ActionResult Edit().
1:38
It'll accept an Entry, and
1:47
return a call to View
passing in the entry.
1:50
And also add the HttPost attribute.
1:57
The post version of our
EditAction method will look
2:02
very similar to the add
post action method.
2:05
Let's scroll up a bit
to the Add method and
2:08
split our code editor by
clicking on window Split.
2:10
Now, we can scroll the bottom window down
to our Edit post action method, okay.
2:15
First, we need to validate the entry.
2:22
Validate the entry and if the entry
is valid use the repository to
2:29
update the entry and then redirect
the user to the entry's list page.
2:33
If the entry is valid
2:40
Use the repository to update the entry.
2:46
Redirect the user.
2:54
To the entries list page.
2:58
Lastly, we need to populate the activities
select list items ViewBag property.
3:00
Populate the activities select
3:09
list items ViewBag property.
3:13
We can remove the split by dragging the
divider down to the bottom of the editor.
3:18
Now, let's go back and
implement our TODOs.
3:23
To get the entry, we just in to make a
call to our repositories get entry method.
3:27
Entry, entry,equals
3:31
entriesRepository.GetEntry(id).
3:36
Don't forget,
the ID parameter is a nullable int.
3:44
So, we need to cast it to an int in order
to pass it into the get entry method.
3:48
To return a status of not found
if the entry wasn't found,
3:55
we just need to check if
the entry variable is null.
3:59
And if it is,
return a call to the HttpNotFound method.
4:03
If (entry = null)
4:07
return HttpNotFound.
4:12
Remember,, as we saw in an earlier course
every controller inherits from the N.B.C.
4:19
controller base class.
4:25
Which provides us with a number
of convenience methods for
4:27
returning a variety of action results.
4:30
One of those methods is
the HttpNotFound method.
4:33
Our last change to make to this method,
is the easiest one to make.
4:38
We just need to pass the entry
variable into our View method call.
4:41
Now, on to the Post action method.
4:50
First, we need to validate the entry
just like we did in the Add method.
4:53
Here's our server validation rule that
ensures the duration field value is
4:59
greater than 0.
5:04
We could just copy and
paste this code down into our edit method.
5:05
But a better approach would be to
extract this code into its own method.
5:10
Then we could call that method from
the Add an EditPostAction methods.
5:14
Visual Studio makes extracting
code into a method easy to do.
5:19
Just select these lines of code and
5:23
press control period to open
the list of suggested quick actions.
5:25
Then press Enter to select the only
item in the list, extract method.
5:29
Visual Studio will prompt
us to name the new method.
5:34
Let's name the method ValidateEntry and
press enter to complete the refactoring.
5:37
Now, the Add method is calling
the validate entry method
5:45
to validate the entry.
5:49
And here's our new ValidateEntry method.
5:50
The method is using
the private access modifier,
5:55
which makes it visible to just
other members of this class.
5:58
If you'd like to learn more
about access modifiers or
6:02
need a refresher, see the teachers notes
for links to additional resources.
6:05
Visual Studio created the new
method right below our Add method.
6:10
Since this method will be called
from other methods in this class,
6:14
we're gonna move this
method to the bottom.
6:17
Now, we can update the EditPostAction
method with a call to our new
6:22
ValidateEntry method.
6:26
Remember the dry,
don't repeat yourself design principle.
6:31
By not repeating our server validation
code, we're keeping our code dry.
6:36
Another advantage that we gain by
having our service side validation
6:40
in a single method is that if we ever
need to change our validation logic
6:44
we only have to update
our code in one place.
6:49
Let's tackle our next TODO.
6:52
To check of an entry is valid we need
to add an if statement that checks
6:56
if the ModelState IsValid
property is true.
7:00
Then, inside of the if statement we can
call the repositories update entry method.
7:03
And lastly, return a call to the control
or base classes RedirectToAction method
7:09
passing an index for
the action method name to redirect to.
7:15
That completes this TODO.
7:19
For our last TODO,
7:24
we need to populate the activities
select list items ViewBag propert.
7:25
And just like our service side
validation the code that we need
7:30
is already available in
the add post action method.
7:33
Actually, this code is
already in two places.
7:37
It's also in our Add get action method.
7:40
That reminds me we need to
populate the activity select list
7:44
items view big property on every request.
7:48
We forgot to do that on
our edit GetAction method.
7:51
Lets add a TODO comment here
to remind us about that.
7:55
TODO Populate the activities
7:57
select list items ViewBag property.
8:03
Let's scroll back up to
the add GetAction method and
8:10
extract the ActivitesSelectListItems
set up code into its own method.
8:13
Select the code, press Ctrl+period,
8:18
press Enter to select
the extract method quick action,
8:23
name the method
SetUp ActivitiesSelectListItems,
8:27
and press Enter to
complete the refactoring.
8:35
And just like we did with
the ValidateEntry method,
8:39
let's move the new method
to the bottom of the class.
8:42
Now we can update the Add
post action method and
8:50
our edit methods to call the new method.
8:53
I'll just copy this line of code and
paste it here.
8:56
Here.
9:04
And here.
9:06
And that completes the changes
to our controller.
9:09
Let's try building our project
by pressing Ctrl+Shift+B.
9:12
Here in the output window we can see
that our project was successfully built.
9:17
If you're using GitHub,
let's commit our changes.
9:21
Enter a commit message of,
Updated the controller
9:26
to support the "Edit Entry" page,
and click the Commit All button.
9:31
After the break we'll turn
our attention to the View.
9:40
You need to sign up for Treehouse in order to download course files.
Sign up