在Umbraco 4.7.1中无法使用Razor宏隐式转换错误

时间:2011-10-06 11:38:48

标签: razor umbraco umbraco-blog

@inherits umbraco.MacroEngines.DynamicNodeContext
@using System
@using System.Linq
@using System.Xml.Linq
@{
dynamic node = new umbraco.MacroEngines.DynamicNode(1121);

 }


 @foreach (dynamic group in node.Children.OrderBy("CreatedDate").Take(2))
 {
<div class="column-holder">
@foreach (var item in group )
{
      <div class="cell">

         <h3>@item.Name</h3>
        <em>@String.Format("{0:dddd, MMMM d yyyy}", item.EventDateTime) - Starts at @String.Format("{0:HH:mm}", item.EventDateTime)</em><br />
        <span>@(Library.Truncate(Library.StripHtml(item.EventDescription), 50, true))</span>
         <p class="readmore-link"><a href="@item.Url" class="link more">read more</a></p>      

       </div>


}
</div>

}

当我尝试在主页上运行上述micro时出现以下错误:

加载Razor Script~ / macroscripts / eventhomepage.cshtml时出错 无法将类型'umbraco.MacroEngines.DynamicNode'隐式转换为'System.Collections.IEnumerable'。存在显式转换(您是否错过了演员?)

1 个答案:

答案 0 :(得分:0)

我使用这个结构做了类似的事情

IEnumerable<DynamicNode> nodeList = new DynamicNode(1121).Descendants(2).Items.OrderBy(x => x.CreateDate).Take(2);
List<DynamicNode> nodes = nodeList.ToList(); 

然后你的foreach就是

foreach(DynamicNode group in nodes)

并确保添加

@using umbraco.MacroEngines