@foreach (var company in employee.Companies)
{
<li>Company - @Html.DisplayFor(m => company.Name)</li> // Inside Loop
}
如何在不使用第一个元素的for循环的情况下访问数据。
<li>Company - @Html.DisplayFor()</li> // Outside Loop
答案 0 :(得分:0)
如果您不希望在公司列表为空时显示列表项:
@model ...
@{
var firstCompany = Model.Employee.Companies.FirstOrDefault();
}
@if (firstCompany != null)
{
<li>
Company - @Html.DisplayFor(x => firstCompany.Name)
</li>
}
除非您有公司名称字符串的疯狂模板,否则为什么不这样做:
@model ...
@{
var firstCompany = Model.Employee.Companies.FirstOrDefault();
}
@if (firstCompany != null)
{
<li>
Company - @firstCompany.Name
</li>
}