Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why model => model.Reason_ID turns to model =>Convert(model.Reason_ID)

I have my own html helper extension, which I use this way

<%=Html.LocalizableLabelFor(model => model.Reason_ID, Register.PurchaseReason) %>

which declared like this.

 public static MvcHtmlString LocalizableLabelFor<T>(this HtmlHelper<T> helper, Expression<Func<T, object>> expr, string captionValue) where T : class {
            return helper.LocalizableLabelFor(ExpressionHelper.GetExpressionText(expr), captionValue);
        }

but when I open it in debugger expr.Body.ToString() will show me Convert(model.Reason_ID). But should model.Reason_ID. That's a big problem, because ExpressionHelper.GetExpressionText(expr) returns empty string.

What strange magic is that? How can I get rid of it?

like image 363
er-v Avatar asked May 29 '10 13:05

er-v


1 Answers

The problem lies with your Func generic types. Replace Func<T,object> with Func<T,S> and you'll be good to go:

public static MvcHtmlString LocalizableLabelFor<T,S>(this HtmlHelper<T> helper, Expression<Func<T, S>> expr, string captionValue) where T : class
{
    // ... code ...
}
like image 108
Shay Friedman Avatar answered Sep 28 '22 02:09

Shay Friedman