簡體   English   中英

如何獲得資源的價值? (超過 1 種語言)

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

我只需要資源的值。 我嘗試過這種方式。 因為我有這個代碼。 但是為什么property的屬性總是null呢? 我認為該屬性是 DisplayName。

我錯了什么? 有沒有更好的方法來獲取資源的價值? 不使用 Model? (它是電子郵件內容的一部分。)

在 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...
    }

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

在此處輸入圖像描述

在此處輸入圖像描述

在此處輸入圖像描述

解決此異常的簡單方法也許將DisplayNameAttribute更改為DisplayAttribute

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

但這不是制作多語言 web 網站的好方法

看到這個鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM