2012-12-29 142 views
1

我想我在這裏失去了一些東西。我有一個ASP.NET MVC 4應用程序。在我看來,我有一個與我的模型綁定的下拉列表。直截了當的東西。asp.net mvc DropDownListFor不填充

@Html.DropDownListFor(x => x.GaragingAddress.State, 
         POSBridge.Helpers.StateAbbreviations(), 
         new {id="ADStreetState"}) 

的輔助函數我參考的是在這裏(它不是最有效的,但現在我專注於功能而不是性能的證明。)

namespace POSBridge 
{ 
    public class Helpers 
    { 
     public static List<SelectListItem> StateAbbreviations(bool includeTerritories = false) 
     { 
      List<string> abbreviations = new List<string> 
      { 
       "", 
       "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", 
       "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", 
       "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" 
      }; 
      if (includeTerritories) 
      { 
       abbreviations.AddRange(new string[] { "AS", "GU", "MP", "PR", "VI", "UM" }); 
      } 

      List<SelectListItem> items = new List<SelectListItem>(); 
      foreach (string s in abbreviations) items.Add(new SelectListItem { Text = s, Value = s }); 
      return items; 
     } 
    } 
} 

當頁面呈現,選擇與狀態列表不被填充。所有的狀態值都顯示在下拉列表中。頁面上的其他下拉列表正在填充而沒有錯誤,並且當我在此處放置斷點並檢查即時窗口中的模型值時,Model.GaragingAddress.State屬性肯定有一個值。因此,我確信它可能與我編寫的Helper函數有關,但我不確定是什麼/如何。

TIA

回答

0

好的,這是奇怪的和意外的,但下面的代碼工作。

@Html.DropDownListFor(x => x.GaragingAddress, 
    new SelectList(POSBridge.Helpers.StateAbbreviations(), 
        "Value", 
        "Text", 
        Model.GaragingAddress.State), 
    new { id="ADStreetState" }) 

我不知道爲什麼這個工程和其他不,但我是誰抱怨如果有什麼工作?