MVC 2:如何使用Html.DropDownListFor?

rya*_*yan 2 c# asp.net-mvc-2

我对我的lambda还不太确定但是为什么以下工作没有?4/MVC2

作品:

// SpotlightsController.cs
public class SpotlightFormViewModel
{

    // props
    public Spotlight Spotlight { get; private set; }
    public SelectList Featured { get; private set; }
    public IDictionary<string, int> feature = new Dictionary<string, int>(){
        {"True", 1},
        {"False", 0},
    };

    // constr
    public SpotlightFormViewModel(Spotlight spotlight)
    {
        Spotlight = spotlight;
        Featured = new SelectList(feature.Keys, spotlight.Featured);
    }
}

// Edit.aspx
<div class="editor-label">
    <label for="Featured">Featured:</label>
</div>
<div class="editor-field">
    <%: Html.DropDownList("Featured", Model.Featured)%>
    <%: Html.ValidationMessage("Featured") %>
</div>
Run Code Online (Sandbox Code Playgroud)

不起作用:

// Compiler Error Message: CS1501: No overload for method 'DropDownListFor' takes 1 arguments
// Edit.aspx
<div class="editor-label">
    <%: Html.LabelFor(model => model.Featured) %>
</div>
<div class="editor-field">
    <%: Html.DropDownListFor(model => model.Featured)%>
    <%: Html.ValidationMessageFor(model => model.Featured) %>
</div>
Run Code Online (Sandbox Code Playgroud)

tva*_*son 5

DropDownListFor接受(至少)两个参数.第一个参数是将在回发时保留所选值的属性(并包含当前选定的值),第二个参数是IEnumerable<SelectListItem>包含选项的键/值对的属性.将Feature属性重命名为FeatureMenu或其他内容,并创建属性名称与该选项的值对应的类型.然后将FeatureMenu添加到DropDownListFor的参数中.

 public SelectList FeatureMenu { get; private set; }
 public string Featured { get; private set; }
Run Code Online (Sandbox Code Playgroud)

...

 <%: Html.DropDownListFor( model => model.Featured, Model.FeatureMenu ) %>
Run Code Online (Sandbox Code Playgroud)