9

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 am unable to write the following

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

I will get the following error:-

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

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

Thanks

1
  • Try replacing var with the type of info in the foreach Commented Nov 15, 2013 at 0:06

1 Answer 1

18

DisplayFor needs an expression. Try this:

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

Here, model is a lambda expression parameter, and even though it's not actually used in the lambda expression, this syntax is needed to construct the expression.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.