lte__ lte__ - 1 year ago 75
ASP.NET (C#) Question

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
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download