This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Let's start with reviewing my solution for fixing the field labels. Then we'll review a technique utilizing view models for passing data from our action methods to our views.
Follow Along
To follow along committing your changes to this course, you'll need to fork the dotnet-comic-book-library-manager repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork>
cd dotnet-comic-book-library-manager
git checkout tags/v1.5 -b reviewing-the-provided-project-code
Keyboard Shortcuts
-
CTRL+PERIOD
orCTRL+DOT
- Quick Actions
Other ASP.NET MVC Treehouse Resources
For more information on ASP.NET MVC, see these Treehouse courses.
Additional Learning
Overview of the Comic Books Data Model
Here's an overview of each of the entities in our data model.
-
ComicBook
- This is our main entity, which represents a comic book in our application. -
Series
- A series is a collection of comic books, typically centered on a single main character or story (i.e. "The Amazing Spider-Man" or "Superman"). Each comic book is associated with a single series. -
Artist
- Represents an artist (i.e. "Stan Lee") in our application. -
Role
- Represents an artist's role on a comic book (i.e. "Script" or "Pencils"). It's not unusual for an artist to have more than one role per comic book. -
ComicBookArtist
- This is a bridge entity that allows us to associate a comic book with more than one artist, and each artist with more than one comic book. This entity defines aRoleId
property in addition to theArtistId
property so each artist can be assigned a role for that comic book.
Custom Routes
In order to support the creation of pages for creating and deleting comic book artist records, we needed a way to represent the comic book ID in the URL, in addition to the ID of the child record. For example, when deleting a comic book artist record, we need the both the comic book ID and the comic book artist ID.
To support this using MVC's URL routing, we needed a custom route:
routes.MapRoute(
name: "ComicBookArtists",
url: "ComicBookArtists/{action}/{comicbookid}/{id}",
defaults: new { controller = "ComicBookArtists", action = "Index", id = UrlParameter.Optional }
);
Notice that we hard coded the controller to ComicBookArtists
and the additional route parameter for the comic book ID comicbookid
. By defining this custom route, we're able to have URLs that look like this:
http://comic-book-library-manager.azurewebsites.net/ComicBookArtists/Delete/7/14
Which map to this controller action method (the comicBookId
parameter would have a value of "7" and the id
parameter would have a value of "14"):
public ActionResult Delete(int comicBookId, int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var comicBookArtist = Context.ComicBookArtists
.Include(cba => cba.Artist)
.Include(cba => cba.Role)
.Include(cba => cba.ComicBook.Series)
.Where(cba => cba.Id == id)
.SingleOrDefault();
if (comicBookArtist == null)
{
return HttpNotFound();
}
return View(comicBookArtist);
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up