0

Yesterday I asked the following question Html.Dropdown - get the the value of each option to be the same as the text? which was answered when I found the following post What HTML helper do I use to create a simple dropdownlist that doesn't take in any variables? now that's all well and good however, how would I select a default value (like monthly), here's my code...

<%: Html.DropDownList("day", new SelectList(
    new Dictionary<string,string> { { "Weekly", "Weekly" }, { "Monthly", "Monthly" }, { "Quarterly", "Quarterly" }, { "Annually", "Annually" } },
    "Key", "Value"))
%>

Any help would be appreciated as I haven't found an example to explain this rather simple requirement, perhaps I should purchase a book on MVC?

1 Answer 1

3

Use this overload

    public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue);

The last parameter is selected value. You could rewrite your method as

 <%: Html.DropDownList("day", new
 SelectList(
     new Dictionary<string,string> { { "Weekly", "Weekly" }, { "Monthly",
 "Monthly" }, { "Quarterly",
 "Quarterly" }, { "Annually",
 "Annually" } },
     "Key", "Value", "Monthly")) %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.