White Island
White Island

Reputation: 2751

How can I make my drop-down list default to the selected value?

I've created a drop-down list for Gender, but I'm having trouble figuring out how to pull the selected value in when I go back to the edit page. Here was my original code:

<div>
    @Html.Caption("Gender", "Gender")
    @Html.DropDownList("Gender", new SelectList(new Dictionary<int,string> {{ 0, " " }, {1, "Male"}, {2, "Female"}}, "Key", "Value"))
</div>

I tried this from another question I came across, but it still doesn't seem to be quite right. What am I missing? Let me know if I need to provide any additional code.

<div>
    @Html.Caption("Gender", "Gender")
    @Html.DropDownList("Gender", new SelectList(new Dictionary<int,string> {{ 0, " " }, {1, "Male"}, {2, "Female"}}, "Key", "Value"), Model.Candidate.Gender)
</div>

Thanks!

Upvotes: 1

Views: 986

Answers (1)

user610217
user610217

Reputation:

The SelectList constructor has an overload that accepts the selected item that you can specify.

http://msdn.microsoft.com/en-us/library/dd492553.aspx

Upvotes: 2

Related Questions