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

后端 未结 3 999
深忆病人
深忆病人 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();
       
             //etc....
       @foreach (var item in Model.AssignmentDefinitions)
       {
            
           //etc...
       }
       
    @Html.DisplayNameFor(m => meta.Title) @Html.DisplayNameFor(m => meta.HasMustard)
    @Html.DisplayFor(m => item.Title) @Html.DisplayFor(m => item.HasMustard)
    } else { @:No burgers available. Create(usually make this an action link) a new one. }

提交回复
热议问题