【发布时间】:2013-01-28 20:49:59
【问题描述】:
我很难找到一种干净的方法来做到这一点。我有一个 ViewModel 类,其中包含一个表的行集合。我希望能够在 strongly-typed 集合中的类型上执行 @Html.DisplayNameFor() 而无需引用集合中的第一项或创建该类型的新实例。所以为了澄清这里是一个例子:
public class TableViewModel
{
public string Title { get; set; }
public IEnumerable<Row> Rows { get; set; }
}
public class Row
{
public int ColumnA { get; set; }
public int ColumnB { get; set; }
}
//In the Razor view
<table>
<tr>
<th>@Html.DisplayNameFor(???)</th>
</tr>
</table>
有没有办法在不使用@Html.DisplayNameFor(x => x.Rows.First().ColumnA) 的情况下做到这一点?
【问题讨论】:
-
这是你想要做的吗? stackoverflow.com/questions/3885796/…
-
您可以将
public Row Row { get; set; }添加到您的视图模型中。 -
@LeviBotelho - 我在研究中看到了这个问题,但它并不完全符合要求,因为它不涉及收藏。
-
@Forty-Two - 我想我所设想的是一个辅助方法解决方案。我并不热衷于向我的虚拟机添加额外的属性。
标签: c# asp.net-mvc