有没有办法在ASP MVC3 @Html.DisplayFor()
控件中使用条件?以下是我尝试的语法。虽然看起来partner.TradingPartners
能够使用.Where<>
,但根据Visual Studio,我做错了!
这是我使用的语法
@foreach (var item in Model.Agent.SymNumberToAgentId)
{
var gogo = ViewBag.Periscope + item.AgentId.Trim();
<tr>
<td>
@Html.DisplayFor(modelItem => item.DisplayAgentId)
</td>
<td>
@Html.DisplayFor(modelItem => item.CompanyCode)
</td>
<td>
@foreach (var partner in Model.AgentIdDetails)
{
@Html.DisplayFor(modelItem => partner.TradingPartners.Where(item => item.AgentId == partner.AgentId))
}
</td>
<td>
@Html.ActionLink("Remove", "Delete", "SymetraNumberToAgentId", new { id = item.SymetraNumber, aid=item.AgentId}, null)
</td>
<td>
<a href=@gogo target="_blank">View in Periscope</a>
</td>
</tr>
}
,这是错误消息
CS1928: 'System.Collections.Generic.IList<string>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)' has some invalid arguments
答案 0 :(得分:1)
编辑:问题更新后(foreach中的代码):
@foreach (var partner in Model.AgentIdDetails)
{
@Html.DisplayFor(modelItem => partner.TradingPartners.Where(x => x.AgentId == item.AgentId).XYZProperty)
}
您需要在where子句中使用正确的语法。此外,您应该为过滤的贸易伙伴指定属性。
<td>
@foreach (var partner in Model.AgentIdDetails)
{
@Html.DisplayFor(modelItem => partner.TradingPartners.Where(item => item.AgentId == partner.AgentId).XYZProperty)
}
</td>