Foreach无法在"方法组"上运行.你打算调用'方法组'吗?

2 c# asp.net-mvc umbraco

我对C#很陌生,我收到一个我无法理解的错误?

我有一个视图,我想循环一系列节点,所以我正在尝试这样做:

@foreach (var crumb in Model.Breadcrumb)
{
  //My code
}
Run Code Online (Sandbox Code Playgroud)

在我的viewmodel中我有这个:

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;
}
Run Code Online (Sandbox Code Playgroud)

Sel*_*enç 6

如果不添加括号,编译器会将方法视为方法组.如果要调用方法并迭代结果,请使用:

@foreach (var crumb in Model.Breadcrumb(/* your parameters */))
Run Code Online (Sandbox Code Playgroud)