Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 713 posts 954 karma points
    Jul 14, 2011 @ 04:50
    Tom
    1

    Extension Methods not working??

    What it came down to is even with the right using statements etc you can’t hook into the extension methods.. is there something I’m missing?

     

    First tried:

    <umbraco:Macro  runat="server" language="cshtml">
    @using System.Linq
    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
      
      
    @{
      
      
    var collectionItems = @Model.Descendants("individualCollection");
      
    var externalCollectionItems = Model.Descendants("externalCollection");
      
      
      
      
    if(@collectionItems != null && @externalCollectionItems != null)
      
    {
        
    var allCollectionItems = collectionItems.Cast<DynamicNode>().Union(externalCollectionItems.Cast<DynamicNode>()); 
      
        
    if(@allCollectionItems.Any())
        
    {
        
    <ul>
          
    @foreach (var item in @allCollectionItems)
          
    {
            
    <li><a href="@item.Url">@item.Name</a></li>
          
    }
        
    </ul>
        
    }
      
    }
      
    }
    </umbraco:Macro>

     

    And got:

    Error loading Razor Script 
    'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'Cast

     

    Then tried:

     

    <umbraco:Macro  runat="server" language="cshtml">
    @using System.Linq
    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
      
      
    @{
      
      
    var collectionItems = @Model.Descendants("individualCollection");
      
    var externalCollectionItems = Model.Descendants("externalCollection");
      
      
      
      
    if(@collectionItems != null && @externalCollectionItems != null)
      
    {
        
    var allCollectionItems = collectionItems.ToArray().Cast<DynamicNode>().Union(externalCollectionItems.ToArray().Cast<DynamicNode>()); 
      
        
    if(@allCollectionItems.Any())
        
    {
        
    <ul>
          
    @foreach (var item in @allCollectionItems)
          
    {
            
    <li><a href="@item.Url">@item.Name</a></li>
          
    }
        
    </ul>
        
    }
      
    }
      
    }
    </umbraco:Macro>

     

    And got:

    Error loading Razor Script 
    'System.Array' does not contain a definition for 'Cast

     

    Then we re-thought it... GOT THE LOGIC RIGHT as well as reduced it down and got:

    <umbraco:Macro  runat="server" language="cshtml">
    @using System.Linq
    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
      
    @{
      
      
    var collectionItems = @Model.Descendants("individualCollection");
      
    var externalCollectionItems = @Model.Descendants("externalCollection");
      
    var allCollectionItems = new List<DynamicNode>();
      
      
    if (@collectionItems != null)
      
    {
        
    allCollectionItems.AddRange(Enumerable.Cast<DynamicNode>(@collectionItems));
      
    }
      
      
    if (@externalCollectionItems != null)
      
    {
        
    allCollectionItems.AddRange(Enumerable.Cast<DynamicNode>(@externalCollectionItems));
      
    }
      
      
    if(@allCollectionItems.Count > 0)
      
    { 
        
    <ul>
          
    @foreach (var item in @allCollectionItems)
          
    {
            
    <li><a href="@item.Url">@item.Name</a></li>
          
    }
        
    </ul>
      
    }
      
    }
    </umbraco:Macro>

     

    And that works…

     

    Basically we couldn’t get extension methods to work!

    Any ideas why they don't work?

Please Sign in or register to post replies

Write your reply to:

Draft