lte__
lte__

Reputation: 7576

Asp.Net MVC Razor Dropdown - Accessing list from ViewModel

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?

Upvotes: 1

Views: 246

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

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.

Upvotes: 2

Related Questions