Christofer Ohlsson
Christofer Ohlsson

Reputation: 3145

Confused about DropDownListFor

I am retrieving a few bunches of int/string pairs that I want to generate dropdown lists for, and subsequently use these filter a collection of items.

Currently, I create these filters as SelectLists in the Controller this way:

foreach (int key in filterChoices.Keys)
                subFilter.Add(new SelectListItem {Value = key.ToString(), Text = filterChoices[key]});

where "filterChoices" holds the id (int) and text (string) as key, value pairs.

Then I'm a bit confused as to what to do. I have tried to put together bits and pieces from various examples, but don't really understand what's going on.

This is what I'm trying currently:

Html.DropDownListFor(m => m.SelectedDimension, new SelectList(Model.DimensionFilter, "Text", "Value", Model.DimensionFilter.First().Text));

The first part, I'm not really sure what its use is, but I am assuming it's the property whose change controls what is being selected in the list? Something along those lines? So I'm trying to set this int to some arbitrary value that corresponds to one filter entry, but to no avail.

No DropDown shows up.

Upvotes: 0

Views: 48

Answers (1)

Ravinder Gujiri
Ravinder Gujiri

Reputation: 1514

just use

@Html.DropDownListFor(m => m.SelectedDimension, Model.DimensionFilter)

if you already created a selectList

Upvotes: 1

Related Questions