1

I tried to use DisplayNameFor without the direct use of the Model View, but using a variable of type Expression<Func<TModel, TValue>>

I thought that using the following function will solve it

    //based on http://stackoverflow.com/questions/16208214/construct-lambdaexpression-for-nested-property-from-string
    public static LambdaExpression createExpression(Type type, string propertyName)
    {
        var param = Expression.Parameter(type, "x");
        Expression body = param;
        foreach (var member in propertyName.Split('.'))
        {
            body = Expression.PropertyOrField(body, member);
        }
        return Expression.Lambda(body, param);
    }

but no... when I use in my view like

    @ {
      Model1 model1 = new Model1() { id = 1, code = "Code1", isActive = true, name = "Name1" };
      System.Linq.Expressions.LambdaExpression exp = Utils.createExpression(model1.GetType(), "id");
    }
    @Html.DisplayNameFor(exp)

edit : it throws a CS0411 compilation error

Any ideas?

tit
  • 599
  • 3
  • 6
  • 25
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Oct 04 '14 at 14:13
  • can you provide error message? – Grundy Oct 04 '14 at 14:16
  • Yes, in french, but you have the error code : CS0411: Les arguments de type pour la méthode 'System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' ne peuvent pas être déduits à partir de l'utilisation. Essayez de spécifier les arguments de type de façon explicite. – tit Oct 04 '14 at 15:20
  • In english : Error CS0411, The type arguments for method 'System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor(System.‌​Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression < System.Func < TModel , TValue >> ) ' can not be inferred from the use . Try specifying the type arguments explicitly – tit Oct 04 '14 at 15:44
  • you try call generic method, exception say that you need pass generic argument explicitly, try something like `@Html.DisplayNameFor(exp)` – Grundy Oct 04 '14 at 19:42
  • Just tried it, got a different compile error : CS1502. I do not understand how to find the right argument of @html.DisplayNameFor(?) – tit Oct 04 '14 at 21:03
  • `DisplayNameFor` it's extension. first generic argument `TModel` detect from `@Html` and equal model for current page – Grundy Oct 06 '14 at 03:49

1 Answers1

0

Try

public static LambdaExpression CreateExpression(Type type, string propertyName)
{
    ParameterExpression parameter = Expression.Parameter(type, "x");
    MemberExpression propertyAccess = Expression.Property(parameter, propertyName);
    return Expression.Lambda(propertyAccess, parameter);
}
artm
  • 8,554
  • 3
  • 26
  • 43
  • I got the same compilation error on the line @Html.DisplayNameFor(exp) explaining that the signature dos not match with System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression> – tit Oct 04 '14 at 12:05