asp.net mvc DropDownListFor没有填充

时间:2012-12-29 00:21:04

标签: asp.net asp.net-mvc-4

我想我在这里遗漏了一些东西。我有一个ASP.NET MVC 4应用程序。在我看来,我有一个与我的模型绑定的下拉列表。直截了当的东西。

@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

1 个答案:

答案 0 :(得分:0)

好的,这很奇怪而且意外,但以下代码有效。

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

我不知道为什么会有效,而另一个则不然,但如果有什么工作可以让我抱怨?