Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:40:58+00:00 2026-06-17T14:40:58+00:00

I have a razor helper method that needs to take in a Func<> that

  • 0

I have a razor helper method that needs to take in a Func<> that will return some HTML content to print out. This is what I originally had:

@helper node(string title, Func<HelperResult> descriptions)
{
    ....
    <div>@descriptions()</div>
    ....
}

@node("title", 
              new Func<HelperResult>(() => 
              {
                 return new HelperResult(
                     @<text>
                     <span>"desc1"</span>
                     <span>"desc2"</span>
                     </text>);
              }))

Unfortunately with this my text never gets printed out. No error either.

So I learned about inline helpers, and changed the calling method to this:

@node("title",                     
              @<text>
              <span>"desc1"</span>
              <span>"desc2"</span>
              </text>)

However now I get a compilation error saying

“Delegate ‘System.Func’ does not
take 1 arguments”.

But I’m not passing in any arguments.

So if I change it to Func<object,HelperResult> and then call it using @descriptions(null) I get the following error:

“Cannot use a lambda expression as an argument to a dynamically
dispatched operation without first casting it to a delegate or
expression tree type”

I’m sure I have something wrong somewhere, but I’m not sure what it is.

Edit: I think I may have solved that problem but it introduces some other issues.

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:

@node("title",                     
              ((Func<dynamic, HelperResult>)(@<text>
              <span>"desc1"</span>
              <span>"desc2"</span>
              </text>))

That works and it prints out the span tags correctly. Unfortunately I have to pass in a useless parameter when calling this Func.

Now the issue I have is that my real function does a bit more than just write some spans. It’s more like this:

@node("title",                     
              ((Func<dynamic, HelperResult>)(@<text>
              <span>@Helpers.Format(resource.Description,"item")</span>
              </text>))

Where @Helpers.Format is another helper and resource is a (dynamic) variable from the page model.

Of course now the code runs but nothing is printed out (inside the <span> tag). I put a breakpoint inside my Format 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.

Since it works well outside of this context, I wonder does Razor’s inline helpers not capture the outer variables?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:40:59+00:00Added an answer on June 17, 2026 at 2:40 pm

    Actually HelperResult is something Microsoft would rather you didn’t use, as evidenced by documentation:

    public class HelperResult : IHtmlString in namespace
    System.Web.WebPages

    Summary: This type/member supports the .NET Framework infrastructure
    and is not intended to be used directly from your code.

    A possible solution to your problem might be to wrap your description function in another helper and then pass that helper as a method group to your node helper, like this:

    @helper Node(string title, Func<HelperResult> descriptions)
    {
        <div>@descriptions()</div>
    }
    
    @helper Description() {
        <span>desc1</span>
        <span>desc2</span>
    }
    
    @Node("title", Description)
    

    In any case, your first idea shouldn’t work because a parameter of type Func is in fact equal to a parameterless function, in which case you need to write the lambda expression like this:

    myFunction( () => doSomething)
    

    So your function call would have been:

    @node("title", () =>                    
                  @<text>
                  <span>"desc1"</span>
                  <span>"desc2"</span>
                  </text>)
    

    Since the future of these helpers is a bit dubious though, I would consider switching to either HtmlHelpers for small snippets of html or Partials for larger chunks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have problem with my html helper extension method in razor view engine.
I have the following razor template html, although I can't figure out how to
I have a mvc3 razor helper that show a component: @helper Component(string params){ <div
I'm using ASP.Net MVC 3 with the Razor engine. I have an html helper
I have created a custom razor helper in my MVC4 Web application that I
I have created a Helper method in MVC3 Razor project as(Helpers.cshtml) @helper Link(string fileName,
I have a Helper method that I need to use across multiple views. In
In razor engine I have used LabelFor helper method to display the name But
I have used this custom Helper in My Razor View. @Html.Link(OpenewWindow, Constants.Value, new {
I've created a new html helper method for creating image tags within the razor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.