How do I display the DisplayAttribute.Description attribute value?

前端 未结 12 1861
醉梦人生
醉梦人生 2020-11-27 03:26

I have a model class, with a property like this:

[Display(Name = \"Phone\", Description=\"Hello World!\")]
public string Phone1 { get; set; }
12条回答
  •  情深已故
    2020-11-27 03:56

    You can always create your own custom extension like this:

        public static MvcHtmlString ToolTipLabel (string resourceKey, string text, bool isRequired, string labelFor = "", string labelId = "",string className="")
        {
            string tooltip = string.Empty;
    
            StringBuilder sb = new StringBuilder();
    
            if (!string.IsNullOrEmpty(resourceKey))
            {
                var resources = GetAllResourceValues();
    
                if (resources.ContainsKey(resourceKey))
                {
                    tooltip = resources[resourceKey].Value;
                }
            }
    
            sb.Append("* {0} 
    ", text); } else { sb.AppendFormat(">{0}
    ", text); } return MvcHtmlString.Create(sb.ToString()); }

    and can get it in view like this:

    @HtmlExtension.ToolTipLabel(" "," ",true," "," "," ")
    

提交回复
热议问题