You cannot bind a drop-down list to a dictionary. It does not make sense. To create a drop-down list, you need 2 things: the scalar property to bind the selected value and the collection to bind the drop-down options. You have only the second of these two things, which is a dictionary. Therefore, you cannot use a strongly typed helper.
You can do the following ugliness:
@Html.DropDownList("SelectedValue", new SelectList(MyDictionary, "Key", "Value"))
but of course, a much better approach would be to use a representation model:
public class MyViewModel { public string SelectedValue { get; set; } public SelectList Values { get; set; } }
which you will fill in in the action of your controller:
public ActionResult Foo() { Dictionary<string, string> dic = ... var model = new MyViewModel { Values = new SelectList(dic, "Key", "Value") }; return View(model); }
and finally, in your strongly typed form:
@model MyViewModel @Html.DropDownListFor(x => x.SelectedValue, Model.Values)
source share