这里的第一个例子是我的代码。 模特中的财产:
[Display(Name = "My Name for Quantity")]
public int Quantity { get; set; }
然后我从控制器发送一些数据到视图,在视图中我使用:
Html.DisplayNameFor(model => model.Quantity)
从上面的这个我得到"我的数量名称"。
问题是。如何获取原始属性名称(Quantity)和属性类型(int)?
答案 0 :(得分:0)
你可能想写一个像这样的方法:
public static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
{
return (propertyExpression.Body as MemberExpression).Member.Name;
}
要使用它,你可以这样写:
var propertyName = GetPropertyName(
() => model.Quantity); // returns "Quantity"