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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:29:05+00:00 2026-06-08T19:29:05+00:00

I’m currently attempting to create an HtmlHelper which takes in the same kind of

  • 0

I’m currently attempting to create an HtmlHelper which takes in the same kind of expression as the built-in helpers LabelFor<>, DisplayFor<>, EditorFor<>, etc. but specifically for enumerated types:

e.g. model => model.MyEnumProperty

I’m new to the whole lambda expression thing and although I have been doing more or less okay so far (with a lot of help from other answers by the SackOverflow community) I’m now getting the following exception while trying to retrieve the object (i.e., model) in the expression:

“variable ‘model’ of type ‘WCSFAMembershipDatabase.Models.Address’ referenced from scope ”, but it is not defined”

public static MvcHtmlString EnumDisplayFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
{
    // memberExp represents "model.MyEnumProperty"
    MemberExpression memberExp = (MemberExpression)expression.Body;
    if (memberExp == null)
    {
        throw new ArgumentException(string.Format(
            "Expression '{0}' refers to a method, not a property.",
            expression.ToString()));
    }

    // modelExp represents "model"
    Expression modelExp = memberExp.Expression;

    // Convert modelExp to a Lambda Expression that can be compiled into a delegate that returns a 'model' object
    Expression<Func<TModel>> modelLambda = Expression.Lambda<Func<TModel>>(modelExp);

    // Compile modelLambda into the delegate
    // The next line is where the exception occurs...
    Func<TModel> modelDel = modelLambda.Compile();

    // Retrieve the 'model' object
    TModel modelVal = modelDel();

    // Compile the original expression into a delegate that accepts a 'model' object and returns the value of 'MyEnumProperty'
    Func<TModel, TEnum> valueDel = expression.Compile();

    // Retrieve 'MyEnumProperty' value
    TEnum value = valueDel(modelVal);

    // return the description or string value of 'MyEnumProperty'
    return MvcHtmlString.Create(GetEnumDescription(value));
}

// Function returns the Description Attribute (if one exists) or the string 
// representation for the specified enum value.
private static string GetEnumDescription<TEnum>(TEnum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

    if ((attributes != null) && (attributes.Length > 0))
        return attributes[0].Description;
    else
        return value.ToString();
}

The expression related code in EnumDisplayFor was cobbled together from details found at the following locations:

  • http://blogs.msdn.com/b/csharpfaq/archive/2010/03/11/how-can-i-get-objects-and-property-values-from-expression-trees.aspx
  • https://stackoverflow.com/a/672212

I did locate a few other questions that mention the same exception in relation to lambda expressions but they were all in a context where someone was manually crafting the expression tree and I couldn’t figure out how the information in the answers might apply to my case.

I would really appreciate if anyone can explain (a) why the exception is occurring and (b) how I can fix it. 🙂

Thanks, in advance.

  • 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-08T19:29:06+00:00Added an answer on June 8, 2026 at 7:29 pm

    As Jon has said, trying to extract the model parameter from the expression doesn’t work, as the expression simply does not have that parameter.

    When you create the lambda model => model.Property you are only saying how do you want your method body to be. That lambda compiles to a delegate Func, or, in other words, a method that needs one parameter and returns a value.

    So in order to call a Func you need to pass one parameter, in this case, the model parameter.

    In your example, your method needs to get the model from somewhere before calling “expression” to get the return value. You can get the current model from HtmlHelper parameter, like this:

    public static MvcHtmlString EnumDisplayFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
        {
            //Get the model
            TModel model = htmlHelper.ViewData.Model;
    
            //Compile expression as Func
            Func<TModel, TEnum> method = expression.Compile();
    
            //Calling compiled expression return TEnum
            TEnum enumValue = method(model);
    
            return MvcHtmlString.Create(GetEnumDescription(enumValue));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from

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.