我有一个视图模型:
public class ObjectiveVM
{
public DateTime DatePeriod { get; set; }
public IList<ObList> obList { get; set; }
public class ObList
{
public int ObjectiveId { get; set; }
public int AnalystId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string AnalystName { get; set; }
public bool Include { get; set; }
}
}
视图模型填充在控制器中,并具有我需要的信息:
在我看来,如何访问表头的DisplayName:
@model IEnumerable<Objectives.ViewModels.ObjectiveVM>
...
...
<th>
@Html.DisplayNameFor(model => model.ObList.Include)
</th>
在上面尝试时,我收到错误:
'ObList': cannot reference a type through an expression; try 'Objectives.ViewModels.ObjectiveVM.ObList' instead
我也尝试过:
@model Objectives.ViewModels.ObjectiveVM
...但又一次收到错误:
'ObList': cannot reference a type through an expression; try 'Objectives.ViewModels.ObjectiveVM.ObList' instead
答案 0 :(得分:1)
请注意,ObList
(带有大写字母O)是一个嵌套类。但是,DisplayNameFor
期望某个类型的现有实例的某些成员可以使用。您可能尝试解决的成员是obList
,因此以下是如何调用其Include
属性:
@Html.DisplayNameFor(model => model.obList[0].Include)