9

我有以下看法:-

@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>)”的类型参数。尝试明确指定类型参数

那么任何人都可以建议,如何在我的 foeach 循环中使用 DisplayFor 吗?

谢谢

4

1 回答 1

17

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

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

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

于 2013-11-14T23:59:49.303 回答