Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Keep being sent to the root.

I am not sure what keeps happening. I thought I followed the video spot on but like half the times I launch the solution, I am directed to the root page instead of my controller.

ComicBookRepository.cs

using ComicBookGallery.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ComicBookGallery.Data
{
    public class ComicBookRepository
    {
        private static ComicBook[] _comicBooks = new ComicBook[]
        {
             new ComicBook()
                {
                    SeriesTitle = "The Amazing Spider-Man",
                    IssueNumber = 700,
                    DescriptionHtml = "<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>",
                    Artists = new Artist[]
                    {
                        new Artist() { Name = "Dan Slott", Role = "Script" },
                        new Artist() { Name = "Humberto Ramos", Role = "Pencils" },
                        new Artist() { Name = "Victor Olazaba", Role = "Inks" },
                        new Artist() { Name = "Edgar Delgado", Role = "Colors" },
                        new Artist() { Name = "Chris Eliopoulos", Role = "Letters" },
                    },
                    Favorite = false
                },
                new ComicBook()
                {
                    SeriesTitle = "The Amazing Spider-Man",
                    IssueNumber = 657,
                    DescriptionHtml = "<p><strong>FF: THREE TIE-IN.</strong> Spider-Man visits the FF for a very private wake--just for family.</p>",
                    Artists = new Artist[]
                    {
                        new Artist() { Name = "Dan Slott", Role = "Script" },
                        new Artist() { Name = "Marcos Martin", Role = "Pencils" },
                        new Artist() { Name = "Marcos Martin", Role = "Inks" },
                        new Artist() { Name = "Muntsa Vicente", Role = "Colors" },
                        new Artist() { Name = "Joe Caramagna", Role = "Letters" }
                    },
                    Favorite = false
                },
                new ComicBook()
                {
                    SeriesTitle = "Bone",
                    IssueNumber = 50,
                    DescriptionHtml = "<p><strong>The Dungeon & The Parapet, Part 1.</strong> Thorn is discovered by Lord Tarsil and the corrupted Stickeaters and thrown into a dungeon with Fone Bone. As she sleeps, a message comes to her about the mysterious \"Crown of Horns\".</p>",
                    Artists = new Artist[]
                    {
                        new Artist() { Name = "Jeff Smith", Role = "Script" },
                        new Artist() { Name = "Jeff Smith", Role = "Pencils" },
                        new Artist() { Name = "Jeff Smith", Role = "Inks" },
                        new Artist() { Name = "Jeff Smith", Role = "Letters" }
                    },
                    Favorite = false
                }

        };

        public ComicBook GetComicBook(int id)
        {
            ComicBook comicBookToReturn = null;

            foreach (var comicBook in _comicBooks)
            {
                if (comicBook.Id == id)
                {
                    comicBookToReturn = comicBook;

                    break;
                }
            }
            return comicBookToReturn;
        }
    }
}

ComicBook.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ComicBookGallery.Models
{
    public class ComicBook
    {
        public int Id { get; set; }
        public string SeriesTitle { get; set; }
        public int IssueNumber { get; set; }
        public string DescriptionHtml { get; set; }
        public Artist[] Artists { get; set; }
        public bool Favorite { get; set; }

        public string DisplayText
        {
            get
            {
                return SeriesTitle + "#" + IssueNumber;
            }
        }

        // series-title-issuenumber.jpg
        public string CoverImageFileName
        {
            get
            {
                return SeriesTitle.Replace(" ", "-")
                    .ToLower() + "-" + IssueNumber + ".jpg";
            }
        }
    }
}

ComicBooksController.cs

using ComicBookGallery.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ComicBookGallery.Controllers
{
    public class ComicBooksController : Controller
    {
        public ActionResult Detail()
        {
            var comicBook = new ComicBook()
            {
                SeriesTitle = "The Amazing Spider-Man",
                IssueNumber = 700,
                DescriptionHtml = "<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>",
                Artists = new Artist[]
                {
                    new Artist() { Name = "Dan Slott", Role = "Script" },
                    new Artist() { Name = "Humberto Ramos", Role = "Pencils" },
                    new Artist() { Name = "Victor Olazaba", Role = "Inks" },
                    new Artist() { Name = "Edgar Delgado", Role = "Colors" },
                    new Artist() { Name = "Chris Eliopoulos", Role = "Letters" },
                }
            };

            return View(comicBook);
        }
    }
}

Details.cshtml

@model ComicBookGallery.Models.ComicBook

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = Model.DisplayText;
}

<h2>@Model.DisplayText</h2>

<div class="row">
    <div class="col-md-6">
        <div class="well">
            <h5><label>Series Title:</label>@Model.SeriesTitle</h5>
            <h5><label>Issue #:</label>@Model.IssueNumber</h5>
            <h5><label>Favorite:</label>@(Model.Favorite ? "Yes" : "No")</h5>
                @if (Model.Artists.Length > 0)
                {
                    <h5>Artists</h5>
                    <div>
                        <ul>
                            @foreach(var artist in Model.Artists)
                            {
                            <li>@artist.Role: @artist.Name</li>
                            }
                        </ul>
                    </div>
                }

        </div>
        <h5>Description:</h5>
        <div>@Html.Raw(Model.DescriptionHtml)</div>
    </div>
    <div class="col-md-6">
        <img src="/Images/@Model.CoverImageFileName"
             alt="@Model.DisplayText" class="img-responsive" />
    </div>
</div>

1 Answer

Steven Parker
Steven Parker
231,008 Points

:point_right: You may just have the wrong file name.

Your controller has a method named "Detail" (singular), but the name of your view file is "Details.cshtml" (plural).

In the video, the view file is named "Detail.cshtml".