簡體   English   中英

MVC 2:如何使用Html.DropDownListFor?

[英]MVC 2: How to use Html.DropDownListFor?

我不確定我的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>

不起作用:

// 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>

DropDownListFor接受(至少)兩個參數。 第一個參數是將在回發時保留所選值的屬性(並包含當前所選值),第二個參數是IEnumerable<SelectListItem>其中包含選項的鍵/值對。 將Feature屬性重命名為FeatureMenu或其他名稱,然后創建與選項的值相對應的類型的屬性名稱Featured。 然后將FeatureMenu添加到DropDownListFor的參數中。

 public SelectList FeatureMenu { get; private set; }
 public string Featured { get; private set; }

...

 <%: Html.DropDownListFor( model => model.Featured, Model.FeatureMenu ) %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM