我是一名C#新手,也遇到了一个无法解决的错误?
我有一个视图,想要循环一系列节点,因此我尝试这样做:
@foreach (var crumb in Model.Breadcrumb)
{
//My code
}
在我的视图模型中,我有这个:
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;
}