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# 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
229,732 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.