Reputation: 73938
How to create a DropDown list from IDictionary using Razor in ASp.Net Mvc3?? I=m trying the following code wit no success.
public IDictionary<string, string> CandidatesList = new Dictionary<string, string>();
Html.DropDownListFor(modal => modal.CandidatesList, new SelectList(Model.CandidatesList, "Value", "Key"))
Upvotes: 0
Views: 122
Reputation: 1038940
Don't bind the dropdown to the same property as the second argument. You must bind it to a primitive type property on your model:
@Html.DropDownListFor(
model => model.SelectedCandidateKey,
new SelectList(Model.CandidatesList, "Value", "Key")
)
where SelectedCandidateKey
must be a string property on your view model which will hold the selected item key.
Think of it this way: when you need a dropdownlist in ASP.NET MVC you have to declare 2 properties on your view model:
IEnumerable<SelectListItem>
property that will hold all the available valuesUpvotes: 1