Search code examples
c#linqhtml-helper

How make this expression for html helpers?


i have two classes

public class LocalizedString {
    public string Ru { get; set; }
    public string Kk { get; set; }
    public string En { get; set; }
}

public class Person {
    public LocalizedString FirstName { get; set; }
    public LocalizedString LastName { get; set; }
}

I need make expression like x => x.FirstName.Ru for @Html.TextBoxFor(x => x.FirstName.Ru)

and for LastName and it must depend from current culture

How do it?


Solution

  •     public static Expression<Func<T, string>> GetMember<T>(
            Expression<Func<T, LocalizedString>> expression) {
            MemberExpression member = expression.Body as MemberExpression;
            PropertyInfo propInfo = member.Member as PropertyInfo;
    
            var param = Expression.Parameter(typeof(T), "x");
            Expression propertyLambda = Expression.Property(param, propInfo.Name);
    
            propertyLambda = Expression.Property(propertyLambda, "Ru");
    
            return Expression.Lambda<Func<T, string>>(propertyLambda, param);
        }