2

The view model:

public class LiquiditySummariesViewModel
{
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
    public DateTime ValueDate { get; set; }

    public List<LiquidityA1B5SummariesModel> ControlGroupsSummaries { get; set; }
}

public class LiquidityA1B5SummariesModel
{
    [DisplayFormat(DataFormatString = "{0:n2}")]
    public decimal Balance { get; set; }
    ...
}

Im trying to access Balance on the view :

 @foreach (var item in Model.ControlGroupsSummaries)
 {
        <tr>
            <td>
                 @Html.DisplayFor(p=>p.ControlGroupsSummaries.Select(z=>z.Balance))
            </td>
        </tr>    
 }

But it's not working:

@Html.DisplayFor(p=>p.ControlGroupsSummaries.Select(z=>z.Balance))

throws

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

I don't know how to fix it. I would like to stick to data annotations so any string.Format solution is not an option.

Jarek Bielicki
  • 856
  • 6
  • 16

1 Answers1

3

You have to iterate your list and call display for on each item:

@foreach (var summary in Model.ControlGroupsSummaries)
{
    @Html.DisplayFor(x => summary.Balance)
}
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444