Foreach无法对“方法组"进行操作.您是否打算调用“方法组"? [英] Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'?

查看:94
本文介绍了Foreach无法对“方法组"进行操作.您是否打算调用“方法组"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,但遇到错误,我无法弄清楚?

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

推荐答案

Model.Breadcurmb 是一种方法,而不是属性或字段,因此按以下方式调用

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

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

这篇关于Foreach无法对“方法组"进行操作.您是否打算调用“方法组"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆