2009-09-25 8 views
2

Ok j'ai le code suivantDropdowns de SelectList en C# (valeur sélectionné)

#region getDurationListDD 
    private List<KeyValuePair<int, int>> getDurationListDD 
    { 
     get 
     { 
      List<KeyValuePair<int, int>> dDur = new List<KeyValuePair<int, int>>(); 
      dDur.Add(new KeyValuePair<int, int>(2, 2)); 
      dDur.Add(new KeyValuePair<int, int>(3, 3)); 
      dDur.Add(new KeyValuePair<int, int>(4, 4)); 
      dDur.Add(new KeyValuePair<int, int>(7, 7)); 
      dDur.Add(new KeyValuePair<int, int>(14, 14)); 
      dDur.Add(new KeyValuePair<int, int>(21, 21)); 

      return dDur; 
     } 
    } 
    #endregion 

Alors ce qui suit dans la principale ActionResult ...

ViewData["changeDuration"] = new SelectList(getDurationListDD, "Key", "Value", Duration); 

ceci sur la vue

Html.DropDownList("changeduration", (SelectList)ViewData["changeDuration"]) 

Maintenant, si la durée a été définie (c'est-à-dire int Duration = 7;) alors je m'attendrais à ce que 7 soit sélectionné, mais pour une raison quelconque, il ne l'est pas. Des indices avant que j'abandonne essayer et faire quelque chose de plus productif?

Ta

Répondre

3

Juste réparé.

Changé:

Html.DropDownList("changeduration", (SelectList)ViewData["changeDuration"]) 

Pour:

Html.DropDownList("cduration", (SelectList)ViewData["changeDuration"]) 

Problème résolu

+0

Vous avez sans doute eu une sorte de nom ambigu dans votre vue ou votre vidéotex et en changeant de « cduration » vous désambiguïsé la valeur. –

+0

Ye Je devine que le nom abiguant était l'utilisation de changeDuration deux fois. –

+0

Le nom du modèle ne peut pas être le même que le nom SelectList. Voir mes tutoriels DDL http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc et http: //blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx – RickAndMSFT

Questions connexes