在每个循环中使用DisplayFor

时间:2013-11-14 23:58:42

标签: c# asp.net-mvc linq html-helper

我有以下观点: -

@foreach(var info in Model.Firewall.FirewallCustomers.OrderBy(a=>a.CustomerName)){

<td>@Html.DisplayFor(info.CustomerVLAN.VLANID)</td>
}

但我无法写下以下内容

@Html.DisplayFor(info.CustomerVLAN.VLANID)</td>

我会收到以下错误: -

  

方法的类型参数   “System.Web.Mvc.Html.DisplayExtensions.DisplayFor(System.Web.Mvc.HtmlHelper,   System.Linq.Expressions.Expression&GT)”   无法从使用中推断出来。尝试指定类型参数   明确地

所以有人可以建议,如何在我的foeach循环中使用DisplayFor?

谢谢

1 个答案:

答案 0 :(得分:17)

DisplayFor需要一个表达式。试试这个:

@foreach(var info in Model.Firewall.FirewallCustomers.OrderBy(a=>a.CustomerName)){
    <td>@Html.DisplayFor(model => info.CustomerVLAN.VLANID)</td>
}

这里,model是一个lambda表达式参数,即使它实际上没有在lambda表达式中使用,也需要这种语法来构造表达式。

相关问题