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 3758108
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:06:39+00:00 2026-05-19T10:06:39+00:00

I am trying to write a helper in Razor that looks like the following:

  • 0

I am trying to write a helper in Razor that looks like the following:

@helper DoSomething<T, U>(Expression<Func<T, U>> expr) where T : class

Unfortunately, the parser thinks that <T is the beginning of an HTML element and I end up with a syntax error. Is it possible to create a helper with Razor that is a generic method? If so, what is the syntax?

  • 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-05-19T10:06:40+00:00Added an answer on May 19, 2026 at 10:06 am

    No, this is not currently possible. You could write a normal HTML helper instead.

    public static MvcHtmlString DoSomething<T, U>(
        this HtmlHelper htmlHelper, 
        Expression<Func<T, U>> expr
    ) where T : class
    {
        ...
    }
    

    and then:

    @(Html.DoSomething<SomeModel, string>(x => x.SomeProperty))
    

    or if you are targeting the model as first generic argument:

    public static MvcHtmlString DoSomething<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper, 
        Expression<Func<TModel, TProperty>> expr
    ) where TModel : class
    {
        ...
    }
    

    which will allow you to invoke it like this (assuming of course that your view is strongly typed, but that’s a safe assumption because all views should be strongly typed anyways :-)):

    @Html.DoSomething(x => x.SomeProperty)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a helper class that represents fields on an object. The
I am trying to write a Browser Helper Object (BHO) in C# that manipulates
I'm trying to write my own little HTML helper which acts a lot like
I'm trying to write a test for an UrlHelper extensionmethod that is used like
I'm trying to write a custom Javascript MVC3 Helper class foe my project, and
I am trying to write a method that returns a methodinfo class from a
Trying to write a test for a helper method that uses acts_as_tree. Helper method
I am trying to write a BeginForm style html helper that uses IDisposable to
Using the following: MVC 3 VB.NET RAZOR VIEW engine Html Helper Class In my
I'm trying to write a helper method that accepts the name of a plural

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.