0

Perhaps its my ignorance about how Razor/Html helper works, hopefully someone from this forum will be able to throw light :)

I cant seem to get a simple lamda expression evaluated inside the html helper.

my razor view is strongly bound to the type "BrandViewModel" and then the following code that is supposed to bind the list of brands(Brands property) to a drop down box

@{Html.DropDownListFor((x) => x.BrandId, x.Brands);}

fails with the following error. The name 'x' does not exist in the current context

the intellisense does confirm those properties (BrandId and Brands) exist in the model and it shows the same when i type x.

Thanks in advance.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
thanikkal
  • 3,326
  • 3
  • 27
  • 45

1 Answers1

0
@Html.DropDownListFor((x) => x.BrandId, Model.Brands)

expression ends after first comma and @{Html.DropDownListFor((x) => x.BrandId, Model.Brands)} won't show anything, because @{ } is code block, equivalent to <% %>.. to actually render it, you need to use just @ which is equivalent to <%: %> or write it to output manually.

Lukáš Novotný
  • 9,012
  • 3
  • 38
  • 46
  • Thanks for answering and pointing that out. but the error still stays... The name 'x' does not exist in the current context :( – thanikkal Feb 25 '11 at 13:23
  • @Html.DropDownListFor((x) => x.BrandId, **Model**.Brands) - did you change this? You are using `x.Brands` outside of expression, correct is `Model.Brands`. – Lukáš Novotný Feb 25 '11 at 13:33
  • oops... overlooked that. thank you Lukas... marking this as answer :) – thanikkal Feb 25 '11 at 17:57