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

  • SEARCH
  • Home
  • 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 7932011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:53:11+00:00 2026-06-03T20:53:11+00:00

I have an HtmlHelper like this: (simplified for clarity) public static MvcHtmlString MyHelper<TModel>(this HtmlHelper<TModel>

  • 0

I have an HtmlHelper like this: (simplified for clarity)

public static MvcHtmlString MyHelper<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, object>> expressionId)
{
    // do something here

    tagBuilder.Append(htmlHelper.HiddenFor(expressionId));

    // do something here
}

What happens is that the code works fine when expressionId returns string. But when it returns int, Html.HiddenFor trigger this error:

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

I don’t know exactly what the problem is, but I suspect it has something to do with this:
When I inspect expressionId in debug mode, I can see the expression is: m => Convert(m.Id) instead of the expected m => m.Id.

What bugs me is that, MyHelper is receiving the exact same argument type as HiddenFor. So why can’t I repass that argument?

What should I do?

PS

I’m not passing any nullable expression into MyHelper (I’ve seen some other questions in which this was the answer)

  • 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-03T20:53:13+00:00Added an answer on June 3, 2026 at 8:53 pm

    The problem is with the cast expression of the non-nullable type.

    Try using a strongly typed helper instead of object:

    public static MvcHtmlString MyHelper<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper, 
        Expression<Func<TModel, TProperty>> expressionId
    )
    {
        // do something here
    
        tagBuilder.Append(htmlHelper.HiddenFor(expressionId));
    
        // do something here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HtmlHelper extension method that looks like this: public static MvcHtmlString TextBoxWithMaxLengthFor<TModel,
I have this extension method: public static string GetValueFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>>
I have a MVC3 HtmlHelper extension like this: public static MvcHtmlString ShowInfoBar(this HtmlHelper helper,
If I have en extension like that : public static string ImageLink(this HtmlHelper htmlHelper,
I have a model (simplified for relevance) like this: public abstract class TitleCreateModel :
I have built a extention for the LabelFor that looks like this : public
I have an HtmlHelper function that returns a MvcHtmlString and which I'd like to
I have a html helper, if I call it like this: Html.MyHelper(Prop1) I can
I have a HtmlHelper extension method that I would like to apply some logic
I have some doubts about how to concatenate MvcHtmlString instances because of this information

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.