Using DisplayFor inside a for each loop

I have the following view: -

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

But I can not write the following

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

I will get the following error: -

Type arguments for the method "System.Web.Mvc.Html.DisplayExtensions.DisplayFor (System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)" cannot be taken out of use. Try specifying type arguments explicitly

So can anyone advise how to use DisplayFor inside my foeach loop?

thanks

+8
c # linq asp.net-mvc html-helper
source share
1 answer

DisplayFor expression is required. Try the following:

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

Here, model is a parameter of a lambda expression, and even if it is not used mainly in a lambda expression, this syntax is necessary for constructing the expression.

+15
source share

All Articles