我对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)
如果不添加括号,编译器会将方法视为方法组.如果要调用方法并迭代结果,请使用:
@foreach (var crumb in Model.Breadcrumb(/* your parameters */))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7065 次 |
| 最近记录: |