On my ViewData I have a property name Target of type string which I want the user to select from one of the possible targets.
So I added a new property to my ViewData named Targets, of type SelectList, that holds the possible targets.
Now what I need is, on my view, display a DropDownList with all items from Targets and where the selected item is the one which value is contained in Target.
On my controller:
var dict = new Dictionary<string, string>();
dict.Add("Blank", "_blank");
dict.Add("Parent", "_parent");
dict.Add("Self", "_self");
dict.Add("Top", "_top");
viewData.Targets = new SelectList(targets);
shapper
Contributor
3932 Points
3789 Posts
DropDownList ... What am I doing wrong?
Sep 15, 2008 08:57 PM|LINK
Hello,
On my ViewData I have a property name Target of type string which I want the user to select from one of the possible targets.
So I added a new property to my ViewData named Targets, of type SelectList, that holds the possible targets.
Now what I need is, on my view, display a DropDownList with all items from Targets and where the selected item is the one which value is contained in Target.
On my controller:
var dict = new Dictionary<string, string>(); dict.Add("Blank", "_blank"); dict.Add("Parent", "_parent"); dict.Add("Self", "_self"); dict.Add("Top", "_top"); viewData.Targets = new SelectList(targets);Then on my view I have:
<%= Html.DropDownList("", "Targets", ViewData["Targets"] ?? ViewData.Model.Targets) %>What I see in my DropDownList is:
[Blank, _blank], [Top, _top], ...
What am I doing wrong?
And how can I set the default value of the DropDownList to be the value contained in ViewData["Target"]
Thank You,
Miguel