How to get the column titles from the Display(Name=) DataAnnotation for a strongly typed list scaffold view at runtime?

后端 未结 3 992
深忆病人
深忆病人 2020-12-05 08:29

How do I get the [Display(Name=\"Some Title\")] DataAnnotations \"Some Title\" rendered in the List scaffold view\'s output?

I create a strongly typed l

相关标签:
3条回答
  • 2020-12-05 08:37

    This is the pattern I've followed. This assumes Lists are never null, but can be empty, but condition can easily be short circuited for a null list. I do like Brian's extension method though.

    @if (Model.BurgersList.Count > 0)
    {
      var meta = Model.BurgersList.First();
       <table>
            <tr>
                <th>
                    @Html.DisplayNameFor(m => meta.Title)
                </th>
                <th>
                    @Html.DisplayNameFor(m => meta.HasMustard)
                </th>
             //etc....
       @foreach (var item in Model.AssignmentDefinitions)
       {
            <tr>
                <td>
                    @Html.DisplayFor(m => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(m => item.HasMustard)
                </td>
           //etc...
       }
       </table>
    }
    else
    { 
        @:No burgers available.  Create(usually make this an action link)  a new one.
    }
    
    0 讨论(0)
  • 2020-12-05 08:41
    //MVC4 has the DisplayNameFor Extensions
    
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString DisplayNameFor<TModel, TProperty>(this HtmlHelper<IEnumerable<TModel>> helper, Expression<Func<TModel, TProperty>> expression)
        {
            return DisplayNameFor(expression);
        }
    
        public static MvcHtmlString DisplayNameFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
        {
            return DisplayNameFor(expression);
        }
    
        private static MvcHtmlString DisplayNameFor<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
        {
            var metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, new ViewDataDictionary<TModel>());
            var htmlFieldName = ExpressionHelper.GetExpressionText(expression);
            string s = metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new char[] { '.' }).Last<string>());
            return new MvcHtmlString(HttpUtility.HtmlEncode(s));
        }
    }
    
    0 讨论(0)
  • 2020-12-05 09:03

    Bit of an old topic, but I came up with an extension method to handle this.

    Lets say my model is:

    public class MyModel
    {
        [Display(Name = "Some Property")]
        public string SomeProp { get; set; }
    }
    

    Drop this method into a static class:

    namespace Web.Extensions
    {
        public static class HtmlHelperExtensions
        {
            public static MvcHtmlString DisplayNameFor<TModel, TProperty>(this HtmlHelper<IEnumerable<TModel>> helper, Expression<Func<TModel, TProperty>> expression)
            {
                var name = ExpressionHelper.GetExpressionText(expression);
                name = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
                var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => Activator.CreateInstance<TModel>(), typeof(TModel), name);
                return new MvcHtmlString(metadata.DisplayName);
            }
        } 
    }
    

    Then in your View, which is taking an IEnumerable<MyModel>, you can use it like this:

    @using Web.Extensions
    @model IEnumerable<MyModel>
    
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(m => m.AvgElecCost)
            </th>
    

    The generated HTML will be:

            <th>
                Some Property
            </th>
    

    Hope it helps!

    0 讨论(0)
提交回复
热议问题