I have an IDictionary, now I want to use these values in a selectlist. How do I do this?
Thanks.
I have an IDictionary, now I want to use these values in a selectlist. How do I do this?
Thanks.
Well, IDictionary
inherits from IEnumerable
so you ought to be able to pass that dictionary to one of the constructors for SelectList
.
Just set the Value and Key for dataValueField and dataTextField. You can either do this in your View itself or from your action (havent tested the code below).
var targets = new Dictionary<string, string>(); targets.Add("Blank", "_blank"); targets.Add("Parent", "_parent"); targets.Add("Self", "_self"); targets.Add("Top", "_top"); ViewData["MyList"] = new SelectList(targets, "Key", "Value");