在每个循环中使用DisplayFor

joh*_* Gu 9 c# linq asp.net-mvc html-helper

我有以下观点: -

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

<td>@Html.DisplayFor(info.CustomerVLAN.VLANID)</td>
}
Run Code Online (Sandbox Code Playgroud)

但我无法写下以下内容

@Html.DisplayFor(info.CustomerVLAN.VLANID)</td>
Run Code Online (Sandbox Code Playgroud)

我会收到以下错误: -

无法从用法中推断出方法'System.Web.Mvc.Html.DisplayExtensions.DisplayFor(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression>)'的类型参数.尝试显式指定类型参数

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

谢谢

p.s*_*w.g 17

DisplayFor需要表达.试试这个:

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

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