C# 为控制器中的DropDownListFor设置选定值在razor视图中不起作用

C# 为控制器中的DropDownListFor设置选定值在razor视图中不起作用,c#,asp.net-mvc,razor,asp.net-mvc-5.2,C#,Asp.net Mvc,Razor,Asp.net Mvc 5.2,我的控制器中有: [Route("Checkout")] public ActionResult Checkout() { var sb = new ShoppingBagViewModel(); sb.DestinationCountry = "Netherlands"; // I hoped that this was sufficient sb.CountriesSl.Single(c => c.Text.Equals("Netherlands", Strin

我的控制器中有:

[Route("Checkout")]
public ActionResult Checkout()
{
    var sb = new ShoppingBagViewModel();
    sb.DestinationCountry = "Netherlands"; // I hoped that this was sufficient
    sb.CountriesSl.Single(c => c.Text.Equals("Netherlands", StringComparison.InvariantCultureIgnoreCase)).Selected = true;
    return View(sb);
}
Visual Studio中的Quick watch确认CountriesSl(类型为
List
)具有选定值。(荷兰)

这是我的观点:

@Html.DropDownListFor(m => m.DestinationCountry, Model.CountriesSl)
DestinationCountry
在我的viewmodel中也是一个字符串道具。 我知道有很多类似的问题,但我看了很多,我就是看不到。 我还尝试:

@Html.DropDownListFor(m => Model.DestinationCountry, Model.CountriesSl)
请不要只是给我答案,还要解释我的错误是什么

编辑以明确问题所在:我在razor视图中获得了一个不错的选项列表,但是没有“选中”的项目,只有第一个。当我查看生成的html时,也没有选择的项目

为Ric编辑

public List<SelectListItem> CountriesSl
{
    get
    {
        List<SelectListItem> _countries = HttpContext.Current.Cache["slCountries"] as List<SelectListItem>;
        if (_countries == null)
        {
            _countries = new List<SelectListItem>();
            foreach (string s in System.IO.File.ReadLines(System.Web.Hosting.HostingEnvironment.MapPath("~/tmpwrite/countries.csv")))
            {
                var tmp = s.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                _countries.Add(new SelectListItem() { Text = tmp[1], Value = tmp[0], Selected = false });
            }
            HttpContext.Current.Cache.Add("slCountries", _countries.OrderBy(c => c.Text).ToList(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(24, 0, 0), System.Web.Caching.CacheItemPriority.Normal, null);
        }
        return _countries;
    }
}
public List CountriesSl
{
得到
{
列表_countries=HttpContext.Current.Cache[“slCountries”]作为列表;
如果(_countries==null)
{
_国家=新名单();
foreach(System.IO.File.ReadLines(System.Web.Hosting.HostingEnvironment.MapPath(“~/tmpwrite/countries.csv”))中的字符串s)
{
var tmp=s.Split(“;”.ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
_Add(new-SelectListItem(){Text=tmp[1],Value=tmp[0],Selected=false});
}
HttpContext.Current.Cache.Add(“slCountries”,_countries.OrderBy(c=>c.Text).ToList(),null,System.Web.Caching.Cache.NoAbsoluteExpiration,new TimeSpan(24,0,0),System.Web.Caching.cacheitemriority.Normal,null);
}
返回国;
}
}

DropDownListFor
尝试自动匹配所提供枚举中接收属性的值。一开始我也有这个问题

而是在viewmodel中提供任何实体列表。我通常使用类似于
IDictionary
的东西。然后创建如下下拉列表:

Html.DropDownListFor(m=>m.DestinationCountry, new SelectList(Model.Countries, "Key", "Value"))

然后,SelectList将自动设置正确的选项,使其键与DestinationCountry匹配。

如何填充您的
列表
?您是否设置了
属性以及
文本
?奇怪的是,现在问题和两个答案的分数都是-1,并且没有理由所有内容都被否决。因此,可能问题是我使用文本值将其设置为选中,而不是值。当我用下拉列表的值而不是selecteditem的文本属性来填充
DestinationCountry
时,可能这一切都已修复。您需要使用您提到的值,我只是想看到您编写代码并检查值==text,在这种情况下它可以工作。Lol我基于文本值并尝试在代码中匹配它。但razor忽略了这一点,只对价值起作用。我将把这作为答案,因为这为我指明了正确的方向。谢谢OP已经有一个属性
IEnumerable
。创建一个
字典
,然后从中创建
IEnumerable
,简直是疯了。这个答案根本没有解决这个问题。