2013-02-07 29 views
6

我試圖在MVC3下拉結合以這種方式工作的。DROPDOWNLIST用於與字典<字符串,字符串>不與所選值

型號

public static Dictionary<string, string> SexPreference() 
{ 
     var dict = new Dictionary<string, string>(); 
     dict.Add("Straight", "S"); 
     dict.Add("Gay", "G"); 
     dict.Add("Bisexual", "BISEX"); 
     dict.Add("Bicurious", "BICUR"); 
     return dict; 
} 

控制器

ViewBag.SexPreference = MemberHandler.SexPreference(); 

查看

@{ 
var itemsSexPreference = new SelectList(ViewBag.SexPreference, "Value", "Key", Model.sexpreference); 
} 

@Html.DropDownListFor(m => m.sexpreference, @itemsSexPreference) 

下拉沒有選擇所選擇的價值,不知道爲什麼。

回答

1

Model.sexpreference必須是下列值之一:直... 這將工作:

public static Dictionary<string, string> SexPreference() 
{ 
    var dict = new Dictionary<string, string>(); 
    dict.Add("S", "Straight"); 
    dict.Add("G", "Gay"); 
    dict.Add("BISEX", "Bisexual"); 
    dict.Add("BICUR", "Bicurious"); 
    return dict; 
} 

@{ 
    var itemsSexPreference = new SelectList(ViewBag.SexPreference, "Key", "Value", "S"); 
} 
+0

值以正確的方式來

var percentagesFromDb = _service1.GetPercentagesFromDb(parameters); var percentages = percentagesFromDb.ToDictionary(percentage => percentage.someKey, percentage => percentage.someValue); ViewData["selectedPercentages"] = percentages; 

然後在模型。像S,G,BISEX等 –

+0

不工作,已經檢查。 –

14

爲什麼你要爲ViewBag.SexPreference,當你有一個模型?忘掉這個ViewBag吧。此外,您應該有2個屬性才能創建下拉列表:標量類型屬性以保存選定值,集合屬性保存選定值的列表。現在你似乎只使用一個,試圖將DropDown綁定到一個集合屬性,這顯然沒有任何意義。

做正確的方式,通過使用視圖模型:

public class MyViewModel 
{ 
    public string SelectedSexPreference { get; set; } 
    public Dictionary<string, string> SexPreferences { get; set; } 
} 

,你會填充你的控制器動作,並傳遞給視圖:

public ActionResult SomeAction() 
{ 
    var model = new MyViewModel(); 

    // Set the value that you want to be preselected 
    model.SelectedSexPreference = "S"; 

    // bind the available values 
    model.SexPreferences = MemberHandler.SexPreference(); 

    return View(model); 
} 

和您的視圖中:

@model MyViewModel 

@Html.DropDownListFor(
    m => m.SelectedSexPreference, 
    new SelectList(Model.SexPreferences, "Value", "Key") 
) 
+1

model.sexpreference是字符串不收集。 –

+0

問題不在於綁定下拉菜單。問題在選定的價值。 –

+1

這是因爲在你的ViewBag你已經把一個集合有相同的名稱作爲模型中的字符串值。如果你想違背的最佳實踐,而不是使用一個視圖模型,我建議你還是可以把它的工作,但你需要重命名ViewBag參數,以便它不與你的字符串屬性名相沖突:'ViewBag.AvailableSexPreferences = MemberHandler。 SexPreference();'。然後視圖內:'@ Html.DropDownListFor(M => m.sexpreference,新的SelectList(ViewBag.AvailableSexPreferences, 「值」, 「鍵」))'。但再次,這不是我會推薦的解決方案。 –

0

我做了不同的看法。這整件事情非常令人沮喪。

在控制器做類似:終於在視圖

[Display(Name = "%")] 
public string Percentage { get; set; } 

private static Dictionary<string, string> GetPercentageList() 
{ 
    return new Dictionary<string, string> 
    { 
     {"100", "100%"}, 
     {"90", "90%"}, 
     {"80", "80%"}, 
     {"70", "70%"}, 
     {"60", "60%"}, 
     {"50", "50%"}, 
     {"40", "40%"}, 
     {"30", "30%"}, 
     {"20", "20%"}, 
     {"10", "10%"} 
    }; 
} 

public SelectList GetSelectList(string selected, object selectedPercentages) 
{ 
    var selectedDict = new Dictionary<string, string>(); 
    if (selectedPercentages != null) 
     selectedDict = (Dictionary < string, string>) selectedPercentages; 
    var selectedvalue = selectedDict.ContainsKey(selected) ? selectedDict[selected] : "100"; 
    Percentage = selectedvalue; 
    return new SelectList(GetPercentageList(), "Key", "Value"); 
} 

然後:

<table class="table table-bordered table-striped"> 
    <thead> 
    <tr> 
     <th>@Html.LabelFor(m => m.Items, new {@class = "control-label"})</th> 
     <th>@Html.LabelFor(m => m.Percentage, new {@class = "control-label"})</th> 
    </tr> 
    </thead> 
    <tbody> 
    @foreach (var item in Model.Items) 
    { 
     <tr> 
      <td> 
       @item.Value 
      </td> 

      <td> 
       @Html.DropDownListFor(m => m.Percentage, 
       Model.GetSelectList(item.Key, ViewData["selectedPercentages"]), 
       new {@class = "form-control", id = item.Key}) 
      </td> 
     </tr> 
    } 
    </tbody> 
</table> 
+0

太遲迴答。 :)不管怎樣,謝謝。 –

相關問題