简体   繁体   中英

How to get the values of the resources? (More than 1 languages)

I just need only the values of the resources. I tried it on this way. Because I've this code. But why is the attribute of the property always null? I think the attribute is the DisplayName.

What do I wrong? Is there a better way to get values of the resources? Without using a Model? (It is a part of the content of an e-mail.)

In the Controller

    private string DisplayName(string value)
    {
      try
      {
         PropertyInfo property = typeof(FormModel).GetProperty(value);
         var attribute = property.GetCustomAttribute(typeof(DisplayNameAttribute), true).Cast<DisplayNameAttribute>().FirstOrDefault();
    
         if (attribute == null)
           return value;
        else
           return attribute.DisplayName;
      }
      catch (Exception ex)
      {
          return value;
       }
    }
    
    private string GetHtmlContent(Dto dto)
    {
        string htmlContent = string.Empty;
    
        htmlContent += $"<h4>{DisplayName("PersonalData")</h4>";
        enz...
    }

The Model:

public class FormModel
{
    [Display(Name = "PersonalData", ResourceType = typeof(StringResource))]
    public string PersonalData { get; set; }

    [Display(Name = "Name", ResourceType = typeof(StringResource))]
    public string Name { get; set; }

    [Display(Name = "Phone", ResourceType = typeof(StringResource))]
    public string Phone { get; set; }

    [Display(Name = "EMail", ResourceType = typeof(StringResource))]
    public string EMail { get; set; }

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

the simple way to solve this exeption Perhaps change DisplayNameAttribute to DisplayAttribute

var attribute = property.GetCustomAttribute(typeof(DisplayAttribute), true).Cast<DisplayAttribute>().FirstOrDefault();

but this is not good way to make multi language web sites

see this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM