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:

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.

Community
  • 1
  • 1

2 Answers2

1

What you're trying to do doesn't make sense. You're trying to find the model object based only (as far as I can see) on the lambda expression.

To put it in more concrete terms, ignoring the enum side of things, if I give you an Expression<string, int> constructed from text => text.Length, there's no specific string that that refers to - but your code would be trying to build and run a Func<string> from that lambda expression. That simply doesn't work.

Basically, you need a model object in order to apply the projection that you're being supplied with. I don't know where you want to get that model object from (perhaps the HtmlHelper parameter that you're currently ignoring?) but you can't just get it from the projection.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • LOL! This is what comes from trying to figure out how Lambda Expressions work "on the fly" rather than doing some solid research on them. Thanks for the explanation. – Jenni Merrifield Aug 01 '12 at 07:53
0

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));
    }
Iñaki Elcoro
  • 2,153
  • 1
  • 18
  • 33
  • Hallelujah! Thank you so much! Your answer provided the extra clarification I needed on Jon's comments AND example code that does EXACTLY what I was trying to figure out how to do. (Now I'm just wishing I'd simply posted a question about how to get the model to use in the compiled expression yesterday afternoon rather than bashing my head against the virtual wall trying to piece together something that might work given my limited understanding of Lambda Expressions. Ah well. Live and learn!) – Jenni Merrifield Aug 01 '12 at 07:55
  • 1
    There's no point in making the selector an expression if all you do is compile and run it, just make it the appropriate delegate type. – Jeff Mercado Aug 01 '12 at 15:40