草庐IT

DisplayAttribute

全部标签

c# - 如何通过反射获取属性的 DisplayAttribute?

我有一个像这样的Helper方法来获取PropertyName(试图避免使用魔法字符串)publicstaticstringGetPropertyName(Expression>expression){varbody=(MemberExpression)expression.Body;returnbody.Member.Name;}但是有时我的PropertyNames也没有很好地命名。所以我宁愿使用DisplayAttribute。[Display(Name="LastName")]publicstringLastname{get;set;}请注意我使用的是Silverlight4.

c# - 从 PropertyInfo 获取 DisplayAttribute 属性

classSomeModel{[Display(Name="QuantityRequired")]publicintQty{get;set;}[Display(Name="CostperItem")]publicintCost{get;set;}}我试图将模型映射到{PropertyName,DisplayName}对列表中,但我遇到了困难。varproperties=typeof(SomeModel).GetProperties().Select(p=>new{p.Name,p.GetCustomAttributes(typeof(DisplayAttribute),false).S

c# - 如何显示 DisplayAttribute.Description 属性值?

我有一个模型类,具有如下属性:[Display(Name="Phone",Description="HelloWorld!")]publicstringPhone1{get;set;}在我的View中显示标签和呈现用于输入的文本框非常简单:@Html.LabelFor(model=>model.Organization.Phone1)@Html.EditorFor(model=>model.Organization.Phone1)@Html.ValidationMessageFor(model=>model.Organization.Phone1)但是如何呈现Description注释