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?