ASP.NET MVC Html.Dropdown - 设置默认值

时间:2011-04-07 10:41:10

标签: c# asp.net-mvc

昨天我问了以下问题Html.Dropdown - get the the value of each option to be the same as the text?,当我找到以下帖子时What HTML helper do I use to create a simple dropdownlist that doesn't take in any variables? 现在这一切都很好,但是,如何选择默认值(如月度),这是我的代码......

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

任何帮助都会受到赞赏,因为我还没有找到解释这个相当简单的要求的例子,也许我应该购买一本关于MVC的书?

1 个答案:

答案 0 :(得分:3)

使用this重载

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

最后一个参数是选中的值。您可以将方法重写为

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