Search code examples
c#asp.net-mvcumbraco

Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'?


I'm pretty new to C# and I'm getting an error I can't quite figure out?

I have a view where I want to loop a series of nodes, so I'm trying do to this:

@foreach (var crumb in Model.Breadcrumb)
{
  //My code
}

As in my viewmodel I have this:

public IEnumerable<LinkModel> Breadcrumb(IPublishedContent content) {
    //Do logic. 
     return GetFrontpage(content, true).Reverse().Select(item => new LinkModel {
        Target = "",
        Text = item.Name,
        Url = item.Url
    }); 
}

private static IEnumerable<IPublishedContent> GetFrontpage(IPublishedContent content, bool includeFrontpage = false)
{
    var path = new List<IPublishedContent>();

    while (content.DocumentTypeAlias != "frontpage")
    {
        if (content == null)
        {
            throw new Exception("No frontpage found");
        }
        path.Add(content);
        content = content.Parent;
    }
    if (includeFrontpage)
    {
        path.Add(content);
    }
    return path;
}

Solution

  • Model.Breadcurmb is a method and not a property or field, so call it as below

         @foreach (var crumb in Model.Breadcrumb(content))