2013-11-14 36 views
8

我有以下看法: -使用DisplayFor內每個循環

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

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

但我無法寫出下面的

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

我會收到以下錯誤: -

The type arguments for method 'System.Web.Mvc.Html.DisplayExtensions.DisplayFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly

因此,任何人都可以建議,如何在我的foeach循環中使用DisplayFor?

由於

+0

嘗試用''foreach''中的''info''類型替換''var'' – acfrancis

回答

15

DisplayFor需要的表達式。試試這個:

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

這裏,model是一個lambda表達式參數,即使它不是實際的lambda表達式中使用,需要此語法來構建表達。