How do I display the DisplayAttribute.Description attribute value?

前端 未结 12 1918
醉梦人生
醉梦人生 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:57

    I ended up with a helper like this:

    using System;
    using System.Linq.Expressions;
    using System.Web.Mvc;
    
    public static class MvcHtmlHelpers
    {
        public static MvcHtmlString DescriptionFor(this HtmlHelper self, Expression> expression)
        {
            var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
            var description = metadata.Description;
    
            return MvcHtmlString.Create(string.Format(@"{0}", description));
        }
    }
    

    Thanks to those who led me in the right direction. :)

提交回复
热议问题