【问题标题】:Get Display attribute of class when displaying in HTML page在 HTML 页面中显示时获取类的显示属性
【发布时间】:2023-04-04 01:19:01
【问题描述】:

这就是我一直在玩的东西。

我有这样的课;

public partial class DespatchRoster : DespatchRosterCompare, IGuidedNav
{
    public string despatchDay { get; set; }
}

我已经向它添加了元数据。

[MetadataType(typeof(RosterMetadata))]
public partial class DespatchRoster
{
}

public class RosterMetadata
{
    [Display(Name="Slappy")]
    public string despatchDay { get; set; }
}    

在我的 HTML 中,我有以下内容;

<% PropertyInfo[] currentFields = typeof(DespatchRoster).GetProperties(); %>

<% foreach (PropertyInfo propertyInfo in currentFields){ %>
  <li class="<%= propertyInfo.Name %>"><%= propertyInfo.Name %></li>
<%} %>

我想看到的是 Slappy 作为 LI 而不是 despatchDay。

我知道我以前做过,但不知道怎么做。

【问题讨论】:

    标签: c# asp.net-mvc system.componentmodel


    【解决方案1】:

    尝试使用this提到的下面的那个。

        private string GetMetaDisplayName(PropertyInfo property)
        {
            var atts = property.DeclaringType.GetCustomAttributes(
                typeof(MetadataTypeAttribute), true);
            if (atts.Length == 0)
                return null;
    
            var metaAttr = atts[0] as MetadataTypeAttribute;
            var metaProperty =
                metaAttr.MetadataClassType.GetProperty(property.Name);
            if (metaProperty == null)
                return null;
            return GetAttributeDisplayName(metaProperty);
        }
    
        private string GetAttributeDisplayName(PropertyInfo property)
        {
            var atts = property.GetCustomAttributes(
                typeof(DisplayNameAttribute), true);
            if (atts.Length == 0)
                return null;
            return (atts[0] as DisplayNameAttribute).DisplayName;
        }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      var properties = typeof(DespatchRoster ).GetProperties()
          .Where(p => p.IsDefined(typeof(DisplayAttribute), false))
          .Select(p => new
              {
                PropertyName = p.Name, p.GetCustomAttributes(typeof(DisplayAttribute),false)
                                .Cast<DisplayAttribute>().Single().Name
              });
      

      【讨论】:

        【解决方案3】:

        试试这个:

        由于您在“正常”MVC 验证或显示模板之外访问元数据,因此您需要自己注册TypeDescription

        [MetadataType(typeof(RosterMetadata))]
        public partial class DespatchRoster
        {
            static DespatchRoster() {
                TypeDescriptor.AddProviderTransparent(
                    new AssociatedMetadataTypeTypeDescriptionProvider(typeof(DespatchRoster), typeof(RosterMetadata)), typeof(DespatchRoster));
            }
        }
        
        public class RosterMetadata
        {
            [Display(Name="Slappy")]
            public string despatchDay { get; set; }
        }
        

        然后要访问显示名称,我们需要使用TypeDescriptor 而不是普通的PropertyInfo 方法枚举属性。

        <% PropertyDescriptorCollection currentFields = TypeDescriptor.GetProperties(typeof(DespatchRoster)); %>
        
        <% foreach (PropertyDescriptor pd in currentFields){ %>
          <% string name = pd.Attributes.OfType<DisplayAttribute>().Select(da => da.Name).FirstOrDefault(); %>
          <li class="<%= name %>"><%= name %></li>
        <%} %>
        

        【讨论】:

          猜你喜欢
          • 2017-06-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-03
          相关资源
          最近更新 更多