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
Let’s update our Comic Book Detail view with the new layout from our designer.
Follow Along
To follow along commiting your changes to this course, you'll need to fork the aspnet-comic-book-gallery repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork>
cd aspnet-comic-book-gallery
git checkout tags/v4.4 -b improving-our-view-layout
Code
Here’s the markup that the designer provided to us for the “Comic Book Detail” view.
<h2>The Amazing Spider-Man #700</h2>
<div class="row">
<div class="col-md-6">
<div class="well">
<h5><label>Series Title:</label> The Amazing Spider-Man</h5>
<h5><label>Issue #:</label> 700</h5>
<h5><label>Favorite:</label> Yes</h5>
<h5>Artists:</h5>
<div>
<ul>
<li>Script: Dan Slott</li>
<li>Pencils: Humberto Ramos</li>
<li>Inks: Victor Olazaba</li>
<li>Colors: Edgar Delgado</li>
<li>Letters: Chris Eliopoulos</li>
</ul>
</div>
</div>
<h5>Description</h5>
<p>Final issue! Witness the final hours of Doctor Octopus' life and his one, last, great act of revenge! Even if Spider-Man survives...<strong>will Peter Parker?</strong></p>
</div>
<div class="col-md-6">
<img src="/Images/the-amazing-spider-man-700.jpg"
alt="The Amazing Spider-Man #700" class="img-responsive" />
</div>
</div>
Additional Learning
For more information on how to build websites using Bootstrap, see this Treehouse course.
The Bootstrap documentation can be found on the official Bootstrap website.
-
0:00
I was just showing our designer the progress that we're making
-
0:03
on our comic book gallery website.
-
0:06
They were happy to see that we integrated their design without having to ask for
-
0:10
any changes.
-
0:12
That being said,
-
0:13
it did feel like our comic book detail view was lacking some pizzazz.
-
0:18
So they decided to work up a new design for that view.
-
0:22
I just got their email with the new layout.
-
0:25
Let's implement those changes now.
-
0:27
Here's the markup that our designers sent me.
-
0:30
If you're following along,
-
0:32
you can get a copy of this markup in the teacher's notes.
-
0:35
There are a couple of different approaches we could take with updating our view.
-
0:40
We could work on updating our existing code with the new layout elements, or
-
0:45
we could bring the new markup into our view and work on making it dynamic.
-
0:49
I'm going to opt for the latter option.
-
0:52
Before we switch to Visual Studio, let's select and
-
0:55
copy this markup to the Clipboard.
-
0:58
Now, let's open the detail Comic View.
-
1:06
Instead of replacing the existing markup, I'm going to create some empty lines
-
1:10
here and paste the contents of the Clipboard into the view.
-
1:14
Let's start at the top and work our way through the new markup.
-
1:17
The first element that we need to update is this heading.
-
1:21
The heading text looks familiar.
-
1:23
Didn't we create a model property that returns the combination
-
1:27
of the series title and issue number?
-
1:29
Let's open our comic book model and review the code.
-
1:33
Okay, I'm not losing my mind.
-
1:35
We did add a display text property that returns the series title and issue number.
-
1:41
Let's update the heading to use that property value for its content.
-
1:48
Continuing on, we encounter a series of div elements with the CSS classes row,
-
1:54
col-md-6, and well.
-
1:58
All three of these CSS classes are part of the Twitter Bootstrap CSS framework.
-
2:04
Bootstrap provides a responsive grid for page layout, the row and
-
2:09
col-md-6 CSS classes are part of this grid system.
-
2:15
The row class creates a grid row and the col-md-6
-
2:20
class creates a column that spans six of the twelve available grid columns.
-
2:26
If we look a little further down in the markup, we'll find the second column
-
2:30
that is also configured to span six of the grid columns.
-
2:34
The well class is used to give an element a simple inset effect.
-
2:38
If we browse to the Bootstrap website,
-
2:41
we can see an example of what this effect looks like.
-
2:44
For more information on the Bootstrap CSS framework, see the teacher's notes for
-
2:49
links to additional resources.
-
2:51
Let's work on updating our well content next.
-
2:54
I'll collapse the solution explorer to give us more room.
-
2:58
Updating the series title, issue number, and
-
3:00
favorite values looks straightforward enough.
-
3:03
Let's replace the text The Amazing Spider-Man with @Model.SeriesTitle.
-
3:10
Replace 700 with @Model.IssueNumber and
-
3:17
replace Yes with @Model.Favorite.
-
3:22
I'm not completely sure about that last one.
-
3:26
Let's go ahead and run our website and see how the favorite property will render.
-
3:30
I'll zoom in to make it easier to see the well content.
-
3:34
It looks like our Boolean property renders as true/false instead of the yes
-
3:38
no text that our design is using.
-
3:41
We need a simple expression to convert our Boolean value to text.
-
3:46
Let's use the C-sharp ternary operator to convert true to yes and false to no.
-
3:54
Okay, save the file and refresh the page.
-
3:59
Wow, did you expect that?
-
4:03
It looks like Razor interpreted the space after the favorite property
-
4:07
as our intention to leave code and switch to writing content.
-
4:11
In these cases, we need to be more explicit with Razor
-
4:14
by surrounding our entire expression with parentheses.
-
4:20
Save the file and refresh the page.
-
4:23
Now it displays as expected.
-
4:26
Sometimes this kind of tweaking is necessary when working with Razor.
-
4:30
Luckily, it's quick and easy to make a change,
-
4:34
refresh the page in the browser, and see the results of the change.
-
4:38
MVC makes this possible by allowing us to make changes to views
-
4:42
without having to stop and restart our website.
-
4:45
Let's finish up the well content by updating the artists lists.
-
4:49
The markup for the new design doesn't look like it has changed from our old markup.
-
4:53
Let's go ahead and
-
4:54
replace the new markup with the old markup from the bottom of the view.
-
4:59
Select the markup that we're interested in.
-
5:01
Be sure to keep the if statement that surrounding the artists lists.
-
5:05
Cut it to the Clipboard, scroll back up to the top of the view,
-
5:09
select the new markup, and paste from the Clipboard.
-
5:14
Lastly let's fix the indenting.
-
5:19
Save the file and refresh the page.
-
5:22
Looks good to go.
-
5:24
Moving down from the well,
-
5:26
it looks like the description is also unchanged from the old markup.
-
5:30
Snake a copy of the description from the old markup like we did for
-
5:34
the artists lists and paste it over the top of the new markup.
-
5:39
We can also remove the rest of the old markup.
-
5:41
Nothing of consequence here now.
-
5:44
That leaves just the comic book cover image to deal with.
-
5:48
So far, we've primarily been using Razor to render content.
-
5:52
But here's an opportunity to see how well it works when working with
-
5:56
HTML elements and attributes.
-
5:59
Remember how we added a property to the comic book model for
-
6:02
our cover image filename?
-
6:04
Now's the time to use it.
-
6:06
Let's replace this text here the-amazing-spider-man-700.jpg
-
6:14
with @Model.CoverImageFileName.
-
6:19
We could also use our displayed text property for the image alternate text.
-
6:27
Save the file, switch back to the browser, and refresh the page.
-
6:32
Once the page is loaded, zoom back out to 100% by pressing control minus.
-
6:39
Great, now we have our view updated with the new design.
-
6:44
While we're here, let's also check out the mobile version.
-
6:47
Right click on any of the page's white space.
-
6:51
Select the Inspect menu item.
-
6:54
And toggle on the device mode if it's not already enabled.
-
6:59
Notice how our grid columns that were displayed side by side in desktop mode
-
7:03
are now stacked on top of each other?
-
7:05
That's the Bootstrap responsive grid in action.
-
7:09
Cool?
-
7:10
Go ahead and close Chrome and stop the website.
-
7:15
If you're using GitHub, let's commit our changes into our commit message
-
7:21
of Updated the Comic Book Detail View Layout and click the Commit All button.
-
7:30
Our website is really starting to come together.
-
7:34
Good job digging further into Razor and updating our comic book detail view.
-
7:39
In the next video,
-
7:40
we're going to turn our attention to an issue that's lurking in our controller
You need to sign up for Treehouse in order to download course files.
Sign up