title: How to pass in a lambda to a Razor helper method?
body: <p>I have a razor helper method that needs to take in a <code>Func&lt;&gt;</code> that will return some HTML content to print out. This is what I originally had:</p>
  
  <pre><code>@helper node(string title, Func&lt;HelperResult&gt; descriptions)
  {
      ....
      &lt;div&gt;@descriptions()&lt;/div&gt;
      ....
  }
  
  @node("title", 
                new Func&lt;HelperResult&gt;(() =&gt; 
                {
                   return new HelperResult(
                       @&lt;text&gt;
                       &lt;span&gt;"desc1"&lt;/span&gt;
                       &lt;span&gt;"desc2"&lt;/span&gt;
                       &lt;/text&gt;);
                }))
  </code></pre>
  
  <p>Unfortunately with this my text never gets printed out. No error either.</p>
  
  <p>So I learned about inline helpers, and changed the calling method to this:</p>
  
  <pre><code>@node("title",                     
                @&lt;text&gt;
                &lt;span&gt;"desc1"&lt;/span&gt;
                &lt;span&gt;"desc2"&lt;/span&gt;
                &lt;/text&gt;)
  </code></pre>
  
  <p>However now I get a compilation error saying </p>
  
  <blockquote>
    <p>"Delegate 'System.Func' does not
    take 1 arguments".</p>
  </blockquote>
  
  <p>But I'm not passing in any arguments. </p>
  
  <p>So if I change it to <code>Func&lt;object,HelperResult&gt;</code> and then call it using <code>@descriptions(null)</code> I get the following error: </p>
  
  <blockquote>
    <p>"Cannot use a lambda expression as an argument to a dynamically
    dispatched operation without first casting it to a delegate or
    expression tree type"</p>
  </blockquote>
  
  <p>I'm sure I have something wrong somewhere, but I'm not sure what it is.</p>
  
  <p><strong>Edit</strong>: I think I may have solved that problem but it introduces some other issues. </p>
  
  <p>What I did was to cast the lambda before passing into a dynamic method. I guess that's what the error was trying to say:</p>
  
  <pre><code>@node("title",                     
                ((Func&lt;dynamic, HelperResult&gt;)(@&lt;text&gt;
                &lt;span&gt;"desc1"&lt;/span&gt;
                &lt;span&gt;"desc2"&lt;/span&gt;
                &lt;/text&gt;))
  </code></pre>
  
  <p>That works and it prints out the <code>span</code> tags correctly. Unfortunately I have to pass in a useless parameter when calling this <code>Func</code>. </p>
  
  <p>Now the issue I have is that my real function does a bit more than just write some spans. It's more like this:</p>
  
  <pre><code>@node("title",                     
                ((Func&lt;dynamic, HelperResult&gt;)(@&lt;text&gt;
                &lt;span&gt;@Helpers.Format(resource.Description,"item")&lt;/span&gt;
                &lt;/text&gt;))
  </code></pre>
  
  <p>Where <code>@Helpers.Format</code> is another helper and resource is a (dynamic) variable from the page model. </p>
  
  <p>Of course now the code runs but nothing is printed out (inside the <code>&lt;span&gt;</code> tag). I put a breakpoint inside my <code>Format</code> helper function, and it hits it and all the parameters are correctly set, so I'm not sure why it wouldn't output correctly. Similarly if I just change it to 
                    resource.Description
  then nothing still gets output. </p>
  
  <p>Since it works well outside of this context, I wonder does Razor's inline helpers not capture the outer variables?</p>