1

I'd like to make a dropdown menu in a cshtml file using Razor. I'm using a ViewModel, in which I define a list. I'd like to use that list's values as dropdown options. For this, I'mtrying:

@model GuestViewModel
...
@Html.DropDownListFor(m => m.SelectedLocation, m => m.travellocations, "Select location!")

However, for the m => m.travellocations (travellocations is a list: travellocations = new List<SelectListItem>();), it just says it cannot convert the lambda to a list. How can I access this list in such a dropdown?

0

1 Answer 1

2

The second parameter is not a lambda expression, so use this declaration:

@Html.DropDownListFor(m => m.SelectedLocation, Model.travellocations, "Select location!")

The lambda expression "m" is a shortcut for Model anyway.

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

1 Comment

Oh thanks, I knew all that stuff except for that I can actually use Model by default :)

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.