Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# ASP.NET MVC Basics Adding a List Page Finishing the List View

I'm getting an error in my Index.cshtml file "Cannot convert method group 'DisplayText' to non-delegate 'dynamic'."

Index.cshtml

@model ComicBookGallery.Models.ComicBook

@{
    ViewBag.Title = Model.DisplayText;
}

<h2>@Model.DisplayText</h2>

<div class="row">
    @foreach (var comicBook in Model)
    {
        <div class="col-md-3">
            <h4>@comicBook.DisplayText</h4>
            <img src="/Images/@comicBook.CoverImageFileName"
                 alt="The Amazing Spider-Man #700"
                 class="img-responsive" />
        </div>
    }
</div>

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";
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
220,634 Points

It looks like this page is set up to expect a single comicBook object as the model:

@model ComicBookGallery.Models.ComicBook
<h2>@Model.DisplayText</h2>

But then later, the code is using the model as an iterable:

    @foreach (var comicBook in Model)
            <h4>@comicBook.DisplayText</h4>

So what does the controller actually give it, a single object or an iterable collection? One or the other of these needs to be adjusted.