Is it possible to create Expression
which can be used in different htmlHelpers (for instance in CheckBoxFor()
), i
Sure:
static Expression<Func<TModel,TProperty>> CreateExpression<TModel,TProperty>(
string propertyName)
{
var param = Expression.Parameter(typeof(TModel), "x");
return Expression.Lambda<Func<TModel, TProperty>>(
Expression.PropertyOrField(param, propertyName), param);
}
then:
var lambda = CreateExpression<SomeModel, bool>("IsAlive");