2011-04-07 72 views
0

昨天我提出以下问题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? 现在这一切都很好但是,我怎么会选择一个默认值(如每月),这里是我的代码...ASP.NET MVC Html.Dropdown - 设置默认值

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

任何帮助,因为我还没有找到一个例子来说明这个简单的要求,将不胜感激,也许我应该对MVC购买一本书吗?

回答

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")) %>