Andy Clarke
Andy Clarke

Reputation: 3262

MVC 3 Razor - Drop down list within Html list

I've got a list of objects that have a Name and a list of values and another property representing the SelectedValue.

I wanted to display the Name and then have a drop down - but not sure how to do it!

Can anyone assist please?

With the following example p.Name works, its the p.Values and p.SelectedValues bits that don't!

<div id="gridProps">
    <ul id="props">
    @foreach (var p in Model.AvailableProperties)
    {
        <li>@p.Name : @Html.DropDownListFor(p.SelectedValue, p.Values)</li>
    }
    </ul>
</div>

Upvotes: 3

Views: 9531

Answers (1)

Sergi Papaseit
Sergi Papaseit

Reputation: 16204

I think what you are looking for is @Html.DropDownList instead of @Html.DropDownListFor

@Html.DropDownList actually has the overload that you're looking for:

@Html.DropDownList(string name, IEnumerable<SelectListItem> selectList)


All of the @Html.DropDownListFor overloads take an Expression<Func<...>> as the first parameter, and it doesn't look like that's what you're passing in your call.

Upvotes: 5

Related Questions