【问题标题】:How make this expression for html helpers?如何为 html 助手制作这个表达式?
【发布时间】:2018-03-06 03:13:37
【问题描述】:

我有两个班级

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; }
}

我需要像x => x.FirstName.Ru这样的表达 为@Html.TextBoxFor(x => x.FirstName.Ru)

对于LastName,它必须依赖于当前文化

怎么做?

【问题讨论】:

  • 你有什么问题?
  • 如何使方法返回我需要的表达式?
  • 在剃刀视图上设置@model Person 使用@Html.TextBoxFor(x => x.FirstName.Ru) 可以做到...
  • 我将有许多类型为本地化字符串的属性,我需要一种方法,它将接受属性名称和模型类型,并用这个属性和语言返回我的表达式,ru kk
  • 那么,您想通过LocalizedString 设置文化吗?

标签: c# linq html-helper


【解决方案1】:
    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);
    }

【讨论】:

    猜你喜欢
    • 2011-08-16
    • 1970-01-01
    • 2011-02-05
    • 2010-11-29
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多